How to Run Shell Scripts in Linux [with Detailed Explanation for Beginners]

What is a shell script? A shell script is a sequence of commands written inside a text file with a “.sh” extension that is intended to be run on Unix-based operating systems like Linux.

Any person skilled in the Linux command line and possessing a decent knowledge of shell and bash scripting languages can create their own shell script to automate certain tasks like backing up the system.

Standard users like you and me, who don’t have any knowledge about this technicality and use Linux just for media or office work, soon you might encounter this shell file and you should know what are the different ways to execute this shell file are and how to choose the relevant one, including which shell interpreter is recommended by the developer.

Stick with this article till the end to learn about them, including bonus tips.

How to Run Shell Scripts in Linux [2 Ways]

There are two ways to execute any shell script in Linux. One is by specifying the script as an argument to your shell, and another is by specifying the script’s absolute or relative path.

For demonstration purposes, I will take the example of a simple shell script that will print “Hello, TRENDOCEANS!” when executed.

Script content
Script content

Above, you can check the content of the script, shown using the cat command. With that due, let’s continue to the next section.

Method 1: Running a Shell Script by Specifying the File as an Argument to Shell

In this method, you need to pass your script file name as an argument to your shell.

For example, if bash is your current shell, then you can pass your script file as an argument to bash like this:

$ bash script.sh

It will process and execute your script, as shown below.

Executing script as argument
Executing a script as an argument

In this method, you have already specified that your script should be executed using the bash interpreter (there are other shell interpreters like Zsh, fish, ksh, etc).

For example, if you have installed and using Zsh, then you can run the same script, as shown below.

$ zsh script.sh

Below is the output of the above command.

Exeuting script using zsh interpreter
Executing a script using the zsh interpreter

Now you might be thinking about which approach you should follow. Let me tell you.

Most of the time, developers write scripts for the bash shell because it is standard and used in many operating systems as the default shell interpreter.

However, operating systems like Kali Linux started using Zsh as the default shell interpreter. To find out which shell interpreter is the default for your operating system, execute the following command.

$ echo $0
#OR
$ echo $SHELL

Below is the output of the above command.

Finding default shell interpreter
Finding a default shell interpreter

As you can see, bash is the default shell interpreter on my Linux system.

To check if any other interpreters are installed on your system, issue the following command.

$ cat /etc/shells

Below is the output of the above command.

Listing all exisiting shell in system
Listing all existing shells in the system

You might be thinking, what is the advantage of passing a shell script as an argument to a shell?

The advantage of using this method is that you don’t have to give executable permission to your script. It will execute the script without any executable permission. As shown below, the executed script does not have any execution permission.

Finding script permissions
Finding script permission

If you follow the next approach, execute the shell script by specifying the absolute or relative path. You need to also give it executable permission.

Method 2: Execute a shell script by specifying its path

In this method, you need to provide a script absolute or relative path with executable permission. If you don’t provide it, it will throw the below error.

Permission denied
Permission denied

To avoid the above error, give your script executable permission using the chmod command, as shown below.

$ chmod +x script.sh

Do not forget to replace “script.sh” with the actual script.

Specifying executable permission to script
Specifying executable permission for the script

As your script now has executable permission, you can execute it again by specifying its absolute or relative path. It will execute the script without any errors.

$ ./script.sh

Below is the output of the above command.

Executing script by specifying relative or absolute path
Executing a script by specifying an absolute or relative path

What is “./” before the script name?

If your script is present in the current directory (which it will be in most cases), then you need to specify ./ before the script name (ex: script.sh). Your execution command will look like this ./script.sh.

Why should you have to do that? When you execute the script without ./ before the script name, it will start searching for script.sh in the environment variable.

It is Linux’s default nature to look for executable files in the selective path inside the environment variable.

Below is the value of the path for my system.

$ echo $PATH

Below is the output of the above command.

Checking envirnment path in Linux
Checking the environment path in Linux

So, whenever I execute the script.sh without ./ in the beginning, it will be searched in the below directories.

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

A file like cat, ip, ls, mv, copy, etc is located in one of those directories. Due to this, you do not have to specify their absolute or relative path. However, your script.sh is not present in any of those directories, and that will become the reason for the error.

So, you should always specify the absolute or relative path of the script. For example, if the script is present in the current directory, then execute the script like this./script.sh, or if it is present inside the Downloads directory, then either you can move into that directory or execute the script by defining a complete path like this ./Downloads/script.sh.

[Bonus Tips!] What is #!/bin/bash at the Beginning of Most Shell Scripts?

When developers specify #!/bin/bash at the beginning of the line in a shell script, it means that they want you to execute the script using the bash interpreter.

To check if this line is present in your shell script, check the script content using the cat command, as shown below.

$ cat script.sh

Below is the output of the above command.

Checking content of the shell script
Checking the content of the shell script

As you can see, in the above script, the first line contains the #!/bin/bash, which means this script should be executed from the bash interpreter.

Is it important to execute that script using bash or can I execute it with other shells like Zsh or fish? The answer to this question is below.

Differences in different shell interpreter
Differences in different shell interpreters

Did you find the difference? If not, then let me tell you. Most of the time, a script can be easily run on different shell interpreters like bash and Zsh. However, there are a few differences between both of them that have been shown above.

In bash, the index number starts with 0, which is the reason the output is Linux Mint, and in Zsh, the index number starts with 1, which is the reason you got the first element Ubuntu in the output.

I hope this makes it clear to you the minor difference between them and why #!/bin/bash is helpful. However, if the developer wants you to run the script in Zsh, then they would define this line like this: #!/bin/zsh.

Wrap Up

If you follow the first method, executing the shell script by passing the file as an argument to the shell, then you have to specify the shell as either bash, zsh, fish, etc.

However, if the developer has written a script specifically for the bash interpreter, then it’s recommended to use bash. You can check the first line of the script. If you find this line #!/bin/bash, then you should execute the script using the bash interpreter.

If you follow the 2nd method, then you don’t have to worry about much. Simply give the script executable permission and execute the script with an absolute or relative path.

I hope this article makes things clear and easier for you to understand how a shell script is executed in different ways and which approach you should follow.

If you have any questions or queries regarding this topic, then feel free to ask them in the comment section.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments