Executing Bash script

Executing a Bash script means running or launching the script in a Bash shell or terminal. Executing a Bash script involves having the Bash interpreter read and execute the commands and instructions written in the script file. In this writing, I will discuss the basics you need to know about executing a bash script.

Why Executing a Bash Script is Necessary?

Enabling the execution of Bash script files is a necessary step for running your Bash programs effectively as they aren’t executable by default. If you don’t make your file executable, you won’t be able to run it directly as a program in Bash. As a result, the commands written inside the script will also not be executed to perform specified tasks.

Necessary Steps for Executing a Bash Script

After you have written a Bash script file and saved the file with a proper name in a desired location, next you need to execute the script, right? Read out the following stepwise tasks you need to do for executing a Bash script:

A. Add Executive Permission

By default, when you create a file in Bash, it does not have the executable permission set. So as the initial step to execute a script or program, you must make it executable by adding executable permission. Otherwise, attempting to execute a non-executable file will result in a “permission denied” error.

Now, the command that is used to add any kind of permissions to any files is the chmod command, & that is no exception for the bash scripts too. And to add the executable permission for a user for any bash script file, use the general command syntax:

chmod u+x script.sh

Adding executive permissionIn the image you can see, I am adding executive permission to my Bash script file named ‘test.sh’.

To learn more about adding executing permission to a Bash script, please go through ‘How to Make a File Executable in Bash’.

B. Run the Bash Script File

Running an executable Bash script file is the final process of getting the desired work done through Bash scripts. And for that, Bash provides various methods to run executable scripts, offering convenience to users. You can run a Bash script by using both GUI & CLI.

With the graphical interface, you can run an executable script file just by right-clicking on it & selecting ‘Run as a Program’ from the context menu.

For the command line interface, you can use several commands such as sh, and bash to execute a script. Moreover, you can also run a script just by specifying its path location. For example, check out the following image where I am running a Bash script named ‘test.sh’ just by specifying its location path (‘./’ is indicating the script is in the current directory).Executing a bash script The script (test.sh) is executed successfully & printing the output.

Learn each of the methods, to run a bash script from ‘How to Run a Bash Script’.

C. Source the Bash Script

In Bash scripting, sourcing a script allows you to run the commands within the script in the current shell session, rather than launching a new subshell. And this entire process is much more helpful when you need to load environment variables, define functions, or execute commands that should affect the current shell session.

You can source a Bash script using the source command or the dot command. The dot command is basically a shorter approach for the source command. In other words, a symbol (.) that represents the source command. Use the command syntax ‘. filename.sh’ while using the dot command or ‘source filename.sh’ for the source command to source a file.

To source a simple script (test.sh) using the source command or dot command just type the following command in your terminal:

source test.sh

OR,

. test.sh

Executing a bash script using the source commandAs you can see, the script is sourced successfully & printing the output message (Hello!) of the script. Same way you can also use the dot operator in place of the source command.

Executing a bash script using the dot command

From the output image you can see, the script is sourced successfully with the dot command & printing the output message (Hello!).

Learn more about sourcing a Bash script using the dot command from “A Complete Overview of Bash Dot Command” & using the source command from “What are the Usages of Bash Source”.

So far from this article, you know how to make a bash script file executable & run that script as the final step of executing a script. Now, check the following application of Bash script where I will talk about running several commands in parallel:

Run Commands in parallel

Running Bash commands in parallel is a technique or method of executing multiple commands simultaneously in a Bash shell environment. This method helps in reducing overall execution time. Moreover, you can process a large number of files and even a complex command that takes a long time to execute. Along with that, it also improves the performance of a Bash script that is running on a single CPU.

Ways to Run Bash Commands in Parallel

Read out the following described ways, through which you can run multiple Bash commands of a Bash script in parallel while executing the script.

1. Ampersand ‘&’ Sign

As a simple approach, use the inherent Bash ampersand (&) operator to run commands in parallel. It allows to execute a command asynchronously, enabling the shell to proceed to the next command without waiting for the current one to finish.

Basic Syntax >

command1&
command2&

2. The “wait” Command With the Ampersand ‘&’ Sign

The wait command waits for all child processes to exit. So using the wait command with the ‘&’ operator we can run batches of operations.

Basic Syntax >

command1&
command2&

wait

3. The “xargs” Command

The xargs command can be used to execute commands in parallel by combining it with the ‘-p’ option. This option specifies the maximum number of parallel processes to run.

4. GNU Parallel

GNU Parallel is a powerful tool that allows you to run commands in parallel. By default, the parallel is not included but you can install GNU Parallel on your system directly from the command line.

Basic Syntax >

parallel ::: prog1 prog2

Here, ‘:::’ is a special syntax that tells the command to iterate over a list of arguments.

To learn each of the methods of running bash commands in parallel with practical examples, please go through the article ‘How to Run Bash Commands in Parallel’.

Conclusion

To sum up, executing Bash scripts lets you automate tasks, making things easier and faster. In this writing, I explained the importance & necessary steps of making a Bash script file executable. Hope this article helps you in understanding the entire process.

People Also Ask

What is script execution?
Script execution refers to the process of running a script, which is a series of commands and instructions written in a programming or scripting language.

What are the steps to execute a shell script?
To execute a shell script, first, create the shell script & save the file with the .sh extension. After that, set the execute permission for the script. Finally, run the Bash script & simply view output or automate scripted tasks.

How to execute a script?
To execute a script, you have to add executable permission to the script. After that, you can run the Bash script & simply view output or automate scripted tasks.

How do I run a bash script step by step?
To run a bash script step by step, first, create the shell script & save the file with the .sh (say, test.sh) extension. After that, set the execute permission for the script using the chmod command (chmod u+x test.sh). Finally, run the Bash script (./test.sh) from your terminal & simply view output or automate scripted tasks.

Related Articles


<< Go Back to Bash Scripting Basics | Bash Scripting Tutorial

5/5 - (1 vote)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment