How To Run A Bash Script {7 Methods}

December 9, 2021

Introduction

Bash scripts are programs that help automate tasks. Scripts store commands that often go together, such as updates and upgrades, to accomplish certain tasks automatically. After creating such programs, there are various ways to run the file and execute the commands through the command line or terminal.

Follow this tutorial to learn how to run a Bash script using various methods.

How to Run a Bash Script

Prerequisites

  • Access to the command line/terminal (CTRL+ALT+T).
  • Essential Linux terminal commands. If you need a refresher, check out our Linux commands cheat sheet.
  • A Bash script file.
  • A text editor, such as Vi/Vim or Nano.

Run Bash Script Using sh

To run a Bash script using sh, enter the following command in the terminal:

sh <script name> <arguments>

For example:

sh script.sh
sh script.sh terminal output

The sh is known as the Bourne shell, and it was the default command line interpreter for older systems. Although not that popular anymore, modern Unix-like systems include the interpreter under /bin/sh.

However, the /bin/sh often links to a different interpreter. To check which interpreter sh uses, run the following command:

ls -l /bin/sh
sh link dash terminal output

The output shows the symbolic link for the sh interpreter. Commonly, Debian and Debian-based systems (such as Ubuntu) link sh to dash, whereas other systems link to bash.

Note: Refer also to our guide that explains how the Bash read command works through various examples.

Run Bash Script using bash

To run a Bash script using bash, enter:

bash <script name> <arguments>

For example:

bash script.sh
bash script.sh terminal output

The bash interpreter is an acronym of Bourne Again Shell and a replacement for the Bourne Shell (sh). The interpreter resides in /bin/bash.

Run Bash Script using source

The source command executes commands from a specified file, including Bash scripts. The general syntax is:

source <path>/<file name> <arguments>

Alternatively, use:

. <path>/<file name>

For example:

source script.sh
. script.sh
run bash script from source terminal output

The path parameter is optional. In that case, the command searches through the directories in the $PATH environment variable. However, if the file is not in $PATH, the command searches the current directory for the file.

Use this method for running Bash scripts within other Bash scripts.

Note: Check out our post on Bash declare statement to learn how to work with variables and their attributes.

Run Bash Script by Specifying the Path

Another way to run a script is to specify the path. This method requires the file's permission to be executable. Without changing the access rights, you get a Permission denied error.  

To make a file executable, run the following command in the terminal:

chmod +x <file name>

Now, execute the script with:

<path to script>/<file name> <arguments>

Different path syntaxes assume the script's location or your current location:

/home/kb/script.sh
run script from absolute path
  • The relative path requires the script to be in the current location:
./script.sh
run script from home folder
  • The shorthand $HOME path requires the script to be in /home/<user>, regardless of your current location:
~/script.sh
run script in home folder

Changing the permissions to executable and specifying the path is the most common and versatile method for running Bash scripts.

Note: Learn about the differences between an absolute path and relative path.

Run Bash Script with Arguments

If a Bash script requires arguments, provide the argument after the script name. All methods for running scripts from the terminal accept user input arguments in the same way.

For example:

./script.sh Hello
bash script.sh Goodbye
run script with arguments

For multiple arguments, separate each by a space.

Note: Learn how to increment or decrement bash variables during script execution.

Run Bash Script using the GUI

To run scripts using the GUI, change the behavior of the file explorer before running the script.

To do so:

1. Open Files and click on the top-right icon.

files icon

2. Select Preferences from the list.

files preferences

3. Click the Behavior tab in the menu. Then, select Ask what to do in the Executable Text Files section.

preferences ask what to do option

4. Close the menu and double-click the script file. A prompt appears with several options.

run script in terminal gui

Select the Run in Terminal option to run the script.

Note: If the terminal closes immediately, add $SHELL to the end of the script to keep the program open.

Run Bash Script from Anywhere

To run a script from anywhere, follow the instructions below.

1. Open the .bashrc file using a text editor:

vim ~/.bashrc

2. Export the script's path to the $PATH environment variable by adding the following line to the .bashrc file:

export PATH="<path to script>:$PATH"

For example:

export PATH="/home/kb/scripts/:$PATH"

Add the line at the very end of the file.

3. Save the changes and close Vim:

:wq

4. Source the .bashrc file to apply the updates:

source ~/.bashrc

5. Lastly, run the script in the terminal from any location by stating the script's name:

script.sh
script.sh terminal output

The method permanently adds the path for the script, making it available in any terminal session.

Note: Learn more about Bash by referring to our post on the Bash let command used for evaluating arithmetic expressions.

Conclusion

After reading this guide, you know how to run a Bash script using various methods. Check out the best Linux text editors to start coding your scripts or learn how to write Bash functions.

If a shell script runs into problems during its execution, resulting in an error signal that interrupts the script unexpectedly it may leave behind temporary files that cause trouble when a user restarts the script. Using Bash trap Command, you can ensure your scripts always exit predictably.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Comment in Bash
November 30, 2021

Comments are an essential part of programming. Learn how to use comments and the best practices to apply to your Bash script commenting techniques.
Read more
Bash Function & How to Use It {Variables, Arguments, Return}
November 3, 2021

Learn how to use functions in Bash scripting through this hands-on tutorial with example codes.
Read more
Bash if elif else Statement: A Comprehensive Tutorial
October 21, 2021

Conditional statements help automate decision making processes in scripts. Learn how to...
Read more
How To Check If File or Directory Exists in Bash
November 29, 2023

Searching for specific files or directories can be time-consuming. You can use a bash command or script to streamline the process. This...
Read more