How to Run a Shell Script in Linux [Essentials Explained for Beginners]

Here are all the essential details you should know about executing a shell script in the Linux command line.
Warp Terminal

There are two ways to run a shell script in Linux. You can use:

bash script.sh

Or you can execute the shell script like this:

./script.sh

That maybe simple, but it doesn’t explain a lot. Don’t worry, I’ll do the necessary explaining with examples so that you understand why a particular syntax is used in the given format while running a shell script.

I am going to use this one line shell script to make things as uncomplicated as possible:

abhishek@itsfoss:~/Scripts$ cat hello.sh 

echo "Hello World!"

Method 1: Running a shell script by passing the file as argument to shell

The first method involves passing the script file name as an argument to the shell.

Considering that bash is the default shell, you can run a script like this:

bash hello.sh

Do you know the advantage of this approach? Your script doesn’t need to have the execute permission. Pretty handy for quick and simple tasks.

Run A Shell Script Linux
Running a Shell Script Linux

If you are not familiar already, I advise you to read my detailed guide on file permission in Linux.

Keep in mind that it needs to be a shell script that you pass as argument. A shell script is composed of commands. If you use a normal text file, it will complain about incorrect commands.

Running Text File As Script in Linux
Running a Text File As Script

In this approach, you explicitly specified that you want to use bash as the interpreter for the script.

Shell is just a program and bash is an implementation of that. There are other such shells program like ksh, zsh, etc. If you have other shells installed, you can use that as well instead of bash.

For example, I installed zsh and used it to run the same script:

Execute Shell Script With Zsh
Execute Shell Script With Zsh

Method 2: Execute shell script by specifying its path

The other method to run a shell script is by providing its path. But for that to be possible, your file must be executable. Otherwise, you’ll have “permission denied” error when you try to execute the script.

So first you need to make sure that your script has the execute permission. You can use the chmod command to give yourself this permission like this:

chmod u+x script.sh

Once your script is executable, all you need to do is to type the file name along with its absolute or relative path. Most often you are in the same directory so you just use it like this:

./script.sh

If you are not in the same directory as your script, you can specify it the absolute or relative path to the script:

Running Shell Script In Other Directory
Running Shell Script In Other Directory

That ./ before the script is important (when you are in the same directory as the script)

Executing Shell Scripts Linux

Why can you not use the script name when you are in the same directory? That is because your Linux systems looks for the executables to run in a few selected directories that are specified in the PATH variable.

Here’s the value of PATH variable for my system:

abhishek@itsfoss:~$ echo $PATH
/home/abhishek/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

This means that any file with execute permissions in one of the following directories can be executed from anywhere in the system:

  • /home/abhishek/.local/bin
  • /usr/local/sbin
  • /usr/local/bin
  • /usr/sbin
  • /usr/bin
  • /sbin
  • /bin
  • /usr/games
  • /usr/local/games
  • /snap/bin

The binaries or executable files for Linux commands like ls, cat etc are located in one of those directories. This is why you are able to run these commands from anywhere on your system just by using their names. See, the ls command is located in /usr/bin directory.

Locating Command Linux

When you specify the script WITHOUT the absolute or relative path, it cannot find it in the directories mentioned in the PATH variable.

Why most shell scripts contain #! /bin/bash at the beginning of the shell scripts?

Remember how I mentioned that shell is just a program and there are different implementations of shells.

When you use the #! /bin/bash, you are specifying that the script is to run with bash as interpreter. If you don’t do that and run a script in ./script.sh manner, it is usually run with whatever shell you are running.

Does it matter? It could. See, most of the shell syntax is common in all kind of shell but some might differ.

For example, the array behavior is different in bash and zsh shells. In zsh, the array index starts at 1 instead of 0.

Bash Vs Zsh
Bash Vs Zsh

Using #! /bin/bash indicates that the script is bash shell script and should be run with bash as interpreter irrespective of the shell which is being used on the system. If you are using zsh specific syntax, you can indicate that it is zsh script by adding #! /bin/zsh as the first line of the script.

The space between #! /bin/bash doesn’t matter. You can also use #!/bin/bash.

Was it helpful?

I hope this article added to your Linux knowledge. If you still have questions or suggestions, please leave a comment.

Expert users can still nitpick this article about things I missed out. But the problem with such beginner topics is that it is not easy to find the right balance of information and avoid having too much or too few details.

If you are interested in learning bash script, we have an entire Bash Beginner Series on our sysadmin focused website Linux Handbook.

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

If you want, you may also purchase the ebook with additional exercises to support Linux Handbook.

Learn Bash Quickly - Bash Beginner Book for Linux Users
This book is available for FREE to Linux Handbook members.Learn Bash Quickly will teach you everything you need to get started with bash scripting. Each bash concept is explained with easy to understand examples. You’ll learn to:Create and run a bash scriptUse variables and pass arguments to scriptU…
About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries 🕵️‍♂️

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

itsfoss happy penguin

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.