Skip to main content

Terminal Basics

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:

  1. Make your shell script executable: chmod a+x
  2. Execute the shell script: ./

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:

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

And finally, you can execute the script by appending your filename to the ./ as shown:


I named my script so I will be using the following:

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 ./

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:

execute shell script using the sh interpreter

Similarly, you can use the bash interpreter:

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:

. ./
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 ./
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.