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
In the image you can see, I am adding executive permission to my Bash script file named ‘test.sh’.
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). The script (test.sh) is executed successfully & printing the output.
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:
As 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.
From the output image you can see, the script is sourced successfully with the dot command & printing the output message (Hello!).
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 >
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 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
- How to make a File Executable in Bash [2 Methods]
- A Complete Overview of Bash Dot Command [With 2 Examples]
- What are the Usages of Bash Source [3 Practical Examples]
- How to Run a Bash Script [2 Methods with Cases]
- How to Run Bash Commands in Parallel [4 Ways with Examples]