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.
- 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>
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
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 <script name> <arguments>
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>
. <path>/<file name>
source script.sh . script.sh
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:
- The absolute path works regardless of the current location:
- The relative path requires the script to be in the current location:
- The shorthand
$HOMEpath requires the script to be in /home/<user>, regardless of your current location:
Changing the permissions to executable and specifying the path is the most common and versatile method for running Bash scripts.
Note: Learn how to write Bash for loop scripts to save time with repeating processes.
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.
./script.sh Hello bash script.sh Goodbye
For multiple arguments, separate each by a space.
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.
2. Select Preferences from the list.
3. Click the Behavior tab in the menu. Then, select Ask what to do in the Executable Text Files section.
4. Close the menu and double-click the script file. A prompt appears with several options.
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:
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"
Add the line at the very end of the file.
3. Save the changes and close Vim:
4. Source the .bashrc file to apply the updates:
5. Lastly, run the script in the terminal from any location by stating the script's name:
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.
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.