Run a Bash Shell Script in Linux Command Line [Beginner's Tip]
Running a bash shell script is quite simple. But you also get to learn about running them in the shell instead of subshell in this tutorial.
β Pranav Krishna
"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
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.
You may also use u+x
which gives execute permission to just you.
Finally, execute the script by this syntax:
./shell_script.sh
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.
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
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
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
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.
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).
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.
Let me know if you have questions or suggestions.
A Computer Science student who happens to love Linux distributions. Loves programming and stands with spending 3 hours trying to automate things instead of doing it by hand.