Run a Bash Shell Script in Linux Command Line [Beginner's Tip]

"Never spend 5 minutes doing something by hand when you can try automating it for 5 hours."

This might be sarcasm to mean that automating might take longer than doing it manually, but automation is necessary for a power Linux user. Shell scripts form a base for automation in Linux.

The simplest way to run a bash shell script is:

bash path_to_script

However, the more popular method is by giving execute permission to the script and then running the script like this:

chmod u+x script.sh
./script.sh

Let me explain this in detail, step by step.

Run Shell Scripts in Ubuntu

First of all, let us create a simple bash script. I'll create a useful bash script that shows the available disk space and RAM:

#!/bin/bash

# Script that shows the available disk space and memory

# Disk space
echo -e "FREE DISK SPACE"
df -h /dev/sda

# Memory (RAM)
echo -e "\nFREE MEMORY"
free -h

# Process ID or PID
echo -e "\nPID = $$"

This script prints the available disk space (in /dev/sda), memory, and the process ID of the shell. I am saving it as "freespace.sh".

To execute this script, you should allow execution permissions for it, which can be done with the chmod command. The syntax is:

chmod +x shell_script.sh
📋
Make sure you type the absolute path of the script file if you're not inside the directory where the script is.

Here, I have given execution permission for all users in the system to execute this script. That is what the +x tag implies. The ls -l command lists the properties of the file, including the permissions.

Changed permissions to execute the script file

You may also use u+x which gives execute permission to just you.

Finally, execute the script by this syntax:

./shell_script.sh
Output of my script (after execution)

Yep, that is a straightforward method to execute a shell script. This applies to all scripts that need to be executed without calling an interpreter!

You can read more about file permissions in Linux here.

Linux File Permissions and Ownership Explained with Examples
Linux file permissions explained in simpler terms. Also learn how to change the file permissions and ownership in Linux in this detailed beginner’s guide.

Alternatively, use an Interpreter

Every scripting language has an interpreter, which directly executes each line, one by one, in the script file. If there's an error in the script file, the execution stops at that particular line (after executing previous lines).

To execute the script with the interpreter, we don't need to change the permissions of the file.

For shell scripts, you can call the shell you're using. Since I am using bash and I've written a bash script, I will be calling sh or bash.

To execute your script, you can call it with an interpreter.

bash ./shell_script.sh

Or

sh ./shell_script.sh
Script ran using the interpreter (bash)

You can type either the relative path or the absolute path here.

Using the source command to run the script in current shell

By default, a shell script runs in a subshell. Sometimes, you may want to run the script in the same shell itself. That's where the source command comes in.

With this command, you include something into the same shell. This is primarily used to update the changes made to files like bashrc, without exiting the shell.

You can execute scripts with it too, like this:

source ./shell_script.sh
Script executed in the same shell

The variable $$ can be used to find the Process ID of the current shell you're using. That's what is done in the example script shown above.

Note that the process IDs (PID) of the script and the parent shell are the same, which implies the script has run in the same shell instead of a new subshell.

The dot operator(.)

Another way to execute scripts in the same shell instead of subshell

The dot (.) represents the shell that you're using which is followed by the script you wanna execute.

. ./shell_script.sh
Script executed in the same shell using the dot operator
What is Subshell in Linux? [Explained]
You might have heard that a shell script runs in its own shell. Learn more on the concept of subshell in Linux.

Bonus Tip: Debug the script while executing

With the interpreter method, you can see what commands are executed, debug for errors in your script, and find the part where the execution takes a hit.

For that, you may use the verbose mode (-v) or xtrace mode (-x) to see what statements are executing.

The verbose mode shows the entire script between the execution of the individual commands. You can take a look at the below image for reference.

Script executed in Verbose mode (-v)

The Xtrace mode is used to trace the execution of each command in the script. The + sign here shows the command before executing it (while ++ is used to depict "executing" commands).

Script executed in Xtrace mode (tracing of execution or -x)

Wrapping up

I hope you liked this basic but essential tutorial on executing bash scripts.

If you are new to Bash scripting, we have a ten chapter Bash Beginner series for you.

Bash Tutorial Series for Beginners: Start Learning Bash Scripting
The 10 chapter series will teach the bash scripting essentials.

Let me know if you have questions or suggestions.