How to Run a sh File in Ubuntu
New to Ubuntu and confused about the .sh file? Learn how to run a .sh file in Ubuntu command line.
You must have come across the files ending with the .sh
extension.
Those files are known as shell scripts, mainly used to automate the process in the terminal.
You can create your own shell scripts too!
The shell script can be executed with two simple steps:
- Make your shell script executable:
chmod a+x shellscript.sh
- Execute the shell script:
./shellscript.sh
Want to learn in more detail? Here's a detailed guide for you!
Run shell scripts on Ubuntu
If you want to learn how you can write shell scripts from scratch, we have a dedicated series for this purpose:
My shell script contains the following lines:
#!/bin/bash
echo "Even if you hate snaps, you'd rarely find a perfect distro like Ubuntu"
echo A controvetial statement by $USER
In the next step, you will have to make your file executable using the chmod command:
chmod a+x shellscript.sh
And finally, you can execute the script by appending your filename to the ./
as shown:
./shellscript.sh
I named my script LU.sh
so I will be using the following:
./LU.sh
But if you are running scripts for installation, mainly while installing packages from source, you will need superuser privileges for the execution:
sudo ./shellscript.sh
Different ways of executing the shell script
If you want to learn more ways of executing a shell script, here you have it:
Using the interpreter
You can execute the shell script by specifying its interpreter.
Such as using the sh
interpreter:
sh shellscript.sh
Similarly, you can use the bash
interpreter:
bash shellscript.sh
Using . ./
(dot space dot slash)
This is the best way when you make changes to a file like .bashrc
that needs logout and log back in to take effect.
So when you execute the script this way, you no longer have to log out or reboot to activate the changes:
. ./shellscript.sh
Using the source command
If you want to use the . ./
(dot space dot slash) but not comfortable with typing dot and space every time, you can use the source command instead.
The source command is a synonym for the dot (.
) so you can have the same effect as dot space dot slash (. ./
) by using source ./
(source space dot slash):
source ./shellscript.sh
Want to learn shell scripting from scratch?
If you want to learn shell scripting from scratch, we have a dedicated series where you will learn from basics to intermediate shell scripting for free:
I hope the given guide will solve any queries you had before.
Still, have some doubts? Let me know in the comments.