Run sh file in Ubuntu
How To

How to Run a sh File in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

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:

  1. Make your shell script executable: chmod a+x shellscript.sh
  2. 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:

How to Create and Run Your First Bash Shell Script
Take the first step towards shell scripting. Learn what it takes to create a simple bash script and how to run it.

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
run a sh file in ubuntu

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
execute shell script using the sh interpreter

Similarly, you can use the bash interpreter:

bash shellscript.sh
execute shell scripts using the bash interpriter

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
enable changes in .basrc file without logging out

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
use source command to execute the shell scripts

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:

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

I hope the given guide will solve any queries you had before.

Still, have some doubts? Let me know in the comments.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.