In Bash, learning different ways to run written scripts is a fundamental need whether for automating repetitive tasks or executing complex commands. Bash provides various methods to run executable scripts, offering convenience to users. From this article, you will learn different ways to run a Bash script file using your system’s both GUI & CLI.
Key Takeaways
- Learning how to run a bash script using both terminal & GUI.
- Getting to know the different cases of CLI to run a Bash script.
Free Downloads
2 Methods to Run a Bash Script
In the following article, I will discuss 2 methods along with some practical cases to show how you can run a Bash script after creating & adding executive permission to it.
Method 01: Run a Bash Script Using Terminal
In the first method, I will show you 5 different cases through which you can run a Bash script using your system’s command line interface.
Case 1: Run a Bash Script Using “sh”
You can simply run a Bash script using ‘sh’. It’s known as the Bourne shell & was the default command line interpreter for older systems. To see the process practically, please go through the following steps:
NOTE: I have used the executable Bash script file ‘test.sh’ in all the examples discussed in this writing. You can download it from the ‘Free Downloads section’. Moreover, if needed you can check articles on ‘How will you start writing a Bash script‘ & ‘How to make a file executable in Bash‘.
Steps to Follow >
❶ At first, open your Ubuntu Terminal application.
❷ Now, let’s check out the contents of the script:
#! /bin/bash
echo=”Hello!”
➌ Afterward, write the following command in your command prompt to run the ‘test.sh’ file:
sh test.sh
➍ Press the ENTER button.
The script runs successfully & displays the output message (Hello!).
Case 2: Run a Bash Script Using the “source” Command
The source (or dot) notation is used to execute a script within the current shell environment. This case is beneficial when you want to retain any changes made by the script, such as modifying environment variables.
To run a script (test.sh) using the source command or dot command just type the following command in your terminal:
source test.sh
OR,
. test.sh
The script runs successfully & displays the output message (Hello!). The script runs successfully & displays the output message (Hello!).
Case 3: Run a Bash Script Using the “bash” Command
You can run a script by invoking the Bash interpreter explicitly. This case is useful when the file does not have the executable permission set. For that, use the bash command.
To run a script (test.sh) using the bash command just type the following command in your command prompt:
bash test.sh
The script runs successfully & displays the output message (Hello!).
Case 4: Run a Bash Script By Specifying the Script Path
In this case, you will try to run a bash script in the most conventional way. Just by specifying the path of the script. This is probably the most used way to run a script file. Now, if the file is situated in the current working directory then just type the script file name with the ‘./’ notation before the file name to represent the current directory.
On the other hand, if the script is located in another location excluding the current directory then specify the full script path with the script name.
NOTE: The absolute path works regardless of the current location. The relative path requires the script to be in the current location.
Write the following command to run a script that is situated in a different location other than the current directory:
. /dir/test.sh
The script runs successfully & displays the output message (Hello!).
Case 5: Run a Bash Script From Anywhere
To run a script from any location of your system by simply typing the script name without even mentioning the script file location path, go through the below steps:
Steps to Follow >
➊ First, make a directory & move the script you want to run from anywhere in your system to that created directory by using the following command:
mkdir bin | mv test.sh /home/munny/bin
- mkdir bin: Creates a directory named ‘bin’.
- Piping ( | ): Redirects the standard output.
- mv: Moves files/directories (source) to the specified destination.
- test.sh: Source script file name.
- /home/munny/bin: Destination directory path location.
➋ After that, open the .bashrc file using the nano text editor:
nano ~/ .bashrc
- nano: Opens a file in the nano text editor.
- ~/.bashrc: Stands for ‘Bash Run Commands’, a script that is automatically executed whenever a new interactive shell is started.
➋ Then, export the script’s path to the $PATH environment variable by adding the following line to the end of the .bashrc file:
export PATH=”/home/munny/bin: $PATH”
- export PATH: Makes the environment variables (here, PATH) available to the child processes for the current shell session.
- /home/munny/bin: Path location of the ‘bin’.
- $PATH: refers to the current value of the ‘PATH’.
➌ Now, save the file using CTRL+S & Exit the nano editor using CTRL+X.
➍ Afterward, source the .bashrc file to update the changes.
source ~/ .bashrc
➍ Finally, run the script in the terminal from any location by only stating the script’s name:
test.sh
The script runs successfully & displays the output message (Hello!).
Method 02: Run a Bash Script Using GUI
In this method, I will show how you can run a Bash script using your Graphical User Interface. Check out the following steps to see how it works:
Steps to Follow >
➊ First, open your File Manager, which is commonly found in the applications menu or the system toolbar. ➋ Locate the Bash file & right-click on it to open the context menu.
➌ After that, the following prompt will show up. And select “Run as a program” to simply run the script. With these simple & easy steps, you can run any of your system’s executable script files.
Comparative Analysis of the Methods
In this article, I showed you how to run your scripts using both GUI & terminal. Anyway, check the following table where I have comparatively analyzed the pros & cons of both methods.
Methods | Pros | Cons |
---|---|---|
Method 1 |
|
|
Method 2 |
|
|
In summary, both methods have their pros and cons. If you have good CLI knowledge and feel more comfortable using it over GUI, use method 1. On the other hand, if you are not sure about commands or facing unknown errors, then use method 2.
Conclusion
To sum up, running an executable Bash script file is the final process of getting the desired work done through Bash scripts. Now, writing & saving a script is useless if you don’t know know how to execute it. In this writing, I discussed several cases, so that you can work with any of them if the other one doesn’t work. Happy scripting!
People Also Ask
Related Articles
- 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 Bash Commands in Parallel [4 Ways with Examples]
<< Go Back to Executing Bash Script | Bash Scripting Basics | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners