How to Run a Shell Script in Linux?

Shell is a command line utility that interprets the user input to display the output. The shell scripts are sequences of user input that are executed by the terminal. The default shell script is also called the bash script; the bash scripts help the users to perform complex tasks using the loops and functions. It improves readability by including comments for the user.

The bash/shell file excites the shell or terminal, which executes the commands in the shell script. The shell script extension is “.sh”. In this article, we will discuss multiple ways to run a shell script. This post covers these topics:

Let’s start the guide!

Prerequisite: How to Create a Shell Script in Linux?

To proceed with the core part of this post, you must have a shell script on which the methods will be tested. If in case you do not have one, then use the below-stated steps to create and customize a shell script:

Step 1: Create a New Bash Script File

Here, we are creating a  bash script with the name “” using the nano editor by writing the following command:

$ nano

If you are using the “vim” editor to create a script named “”, use the following command:

$ vim

Note: The script will be written in this article’s “nano” editor.

Step 2: Add Code to Script

Start adding the bash script from “#!/bin/bash” (which ensures that this will be executed using Bash) representing the bash script code, and then add the commands you want to execute. To run the “ls” command (list the files in the current directory) via bash script, use the following code:


Note: Press the “Ctrl + O” to save the code in nano; next, press “Enter” and “Ctrl + X” to exit the editor.

Step  3: Allow the Execute Permissions for the Script

To run the shell script, the user should have the execute permissions (Normally, the sudo user has the execute permissions by default). To allow the bash script (in this case, to have the execute permissions, use this command:

$ chmod +x

The script is created successfully. The code inside it is shown below:


Now, let’s head over to the methods to run a shell script in Linux.

Method 1: Run Shell Script Using “sh”

To run the shell script (named from the terminal with the “sh”, utilize this command:

$ sh

The output shows the execution of the “ls” command in the bash script code that lists the files of the current directory.

Method 2: Run Shell Script Using “bash”

We can run the shell script with the “bash” command utility with the name of the file “testscript.txt” as given below:

$ bash

To print the content of the bash file and then run the script, the “v” option is used, which stands for “verbose”. To run the “” with displaying its content, execute the below-mentioned command in the terminal:

$ bash -v

Similarly, we can use the “x” option of the bash to print the command in the shell script and executes the command.

$ bash -x

Method 3: Run Shell Script Using “./”

The “./” operator is concatenated with the file name (in this case, testfile.txt) to run the shell script. If you run the shell script without giving the “execute” permissions to the file, it will show the below error:

$ ./

To give execute permissions to the user for “”, utilize this command:

$ chmod +x

Let’s run the “” bash script file after giving the execute permission by using this command utility to execute the shell script:

$ ./

Method 4: Run Shell Script Using “source”

We can use the “source” before the file name to run the bash script. The file named ”” can be executed in the terminal using the below-written command:

$ source

The Dot “.” operator can also be used as a replacement for a source command which has the same meaning. For example, to run the shell script named “”, utilize this command in the terminal:

$ .

Method 5: Run Shell Script Using Absolute Path

To run the shell script, the absolute path can also be used. For instance, a bash script file “” in the home directory (~) execute this command:  

$ ~/

If the script file is in the “Desktop” directory, we can provide the absolute path of the “” to start the bash shell in the Linux, as shown below:

$ ~/Desktop/

The above-stated methods are performed using the “bash” A shell script can also be executed via Z-shell and K-shell.

How to Run a Shell Script Using Z-Shell?

The Z-shell is the advanced version of the bash shell with many new functions like spell check, theme customization, and plugin support. The Z shell is more interactive for users because it allows more customization.

Prerequisite: Install Z-Shell Support

We can install the Z-shell in Linux distros with the following commands:

$ sudo apt install zsh                      #For Debian-based distros.
$ sudo yum install zsh                      #For rpm-based distros.

The syntax for the Z-bash script has “zsh” in the first line instead of “sh”, as in the bash script. To run the “ls” command using the Z-shell, use this command:


Set the execute permissions to the user with the “chmod” command and run the Z shell “Zsh” script using the below commands:

$ sudo chmod +x                        #give execute permissions.
$ /home/itslinuxfoss/                   #run z shell script.

The above picture displays the output of the “ls” command from the Z shell script.

How to Run a Shell Script Using K-Shell?

The K shell abbreviation Korn Shell is the oldest command line interpreter to execute a script from the terminal or file. The K shell provides more programming features like associative arrays, using loops, and print commands, which are widely used by experienced users.

Prerequisite: Install K-Shell Support

The kshell is not pre-installed in Linux. To install the K-shell, use the below-mentioned commands according to your operating system:

$ sudo apt install ksh                       #for Debian-based distros.
$ sudo yum install ksh                       #for rpm based distros.

The method of running the K shell is the same as the bash or Z shell; the only difference between all the shells is writing the script. While writing the script, the K shell starts with the “#!/usr/bin/ksh”, where the “ksh” represents the K shell.

Let’s use the below k-shell (ksh) script for this section:


The k-shell script can be run using the full “path” of the file, as shown below:

$ ~/

The output shows the “ls” command results from the K-shell script.

Bonus Tip: Save the Output of the Shell Script in a File

We can write the desired commands in the bash script and get the output in a file. For instance, to save the output of shell script ( in the “newFile.txt” file, execute this command:

$ sh > newFile.txt

That’s the end.


Several types of shell are used in Linux to run the shell script (code line by line), using “sh”, “bash”, “./”, “source”, Dot “.” and absolute path commands in the terminal. The user must have execute permission to run the shell script. Apart from this, a shell script can be executed using the z-shell and k-shell in Linux, which is also described here. This post has briefly explained the possible methods to run a shell script in Linux.