How to Run a Bash Script [2 Methods With Cases]

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 onHow 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!”

EXPLANATION
The first line, ‘#! /bin/bash’ starts with shebang or hashbang (#!). It indicates the interpreter to be used for executing the script, in this case, it’s bash. Next, the echo command holds a message, ‘Hello!’, which will later be displayed upon the execution of the script.

Afterward, write the following command in your command prompt to run the ‘test.sh’ file:

sh test.sh

Press the ENTER button.

Run a bash script using shThe 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

Run a bash script using source commandThe script runs successfully & displays the output message (Hello!). Run a bash script using dot command 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

Run a bash script using bash commandThe 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

Run a bash script by specifying the script file location path.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
EXPLANATION
  • 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.

Make a directory & move the script file to that directory.➋ After that, open the .bashrc file using the nano text editor:

nano ~/ .bashrc
EXPLANATION
  • 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.

open the bashrc file in the nano editor.➋ 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”
EXPLANATION
  • 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’.

Write inside the bashrc file.➌ 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

Source the updated bashrc file.➍ Finally, run the script in the terminal from any location by only stating the script’s name:

test.sh

Run the bash script file.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. Open file manager using GUI.➋ 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.Run a bash script using GUI. 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

  • Faster process.
  • Provides different ways & options to run a script file.
  • Requires familiarity with CLI.
  • Increased possibility to make mistakes due to manual typos and syntax errors.

Method 2

  • Does not require any knowledge of commands.
  • The visual representation can help beginner-level users.
  • Slower due to clicking and navigation.
  • Does not provide the same level of functionality as the CLI.

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

How can I run bash script?
There are several ways to run a script. You can either use the Command Line Interface with the command syntax “bash file_name.sh” or you can use the Graphical User Interface to right-click on the file & select the option “Run in the terminal” to run the script.
How do I run a .sh script?
To run a bash script after creating you can use the source or dot command. Just use the command syntax, ‘source filename.sh’ or ‘. /filename.sh’.

How do I run a bash script from Command Prompt?
You can use the Command Line Interface with the command syntax “bash file_name.sh” to run the script.

How to use a bash script?
You can effectively use a Bash script to automate tasks, perform actions, or accomplish various objectives within a Linux environment. For using these scripts, you can create & save a script containing the program & later on execute them after adding appropriate permissions.

Related Articles


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

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
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