How to Run Shell Script in Linux?

Reading Time: ( Word Count: )

August 2, 2023

Welcome to our comprehensive guide on how to run shell scripts in Linux. If you’re new to Linux or just starting to explore shell scripting, you’ve come to the right place. In this article, we will walk you through the steps of running shell scripts in Linux, covering all the necessary details to help you become proficient in the process. By the end of this guide, you’ll be equipped with the knowledge to run shell scripts like a pro.

What is a Shell Script?

Before we dive into the technicalities, let’s first understand what a shell script is. A shell script is a script written for the Unix shell, which is a command-line interface for interacting with the operating system. It is a series of commands and instructions that can be executed in sequence to automate tasks, perform system configurations, or accomplish complex operations.

how to run shell script in linux

Choosing the Right Shell

Linux offers various shells, and choosing the right one is crucial for running shell scripts effectively. The most common shells are Bash Bourne Again SHell, Zsh (Z Shell), and Fish (Friendly Interactive Shell). For the purposes of this guide, we will focus on Bash, as it is the default shell for most Linux distributions.

Creating a Shell Script

To create a shell script, you can use a text editor like Nano or Vim or even a graphical text editor like Visual Studio Code. Open your preferred editor and create a new file with a “.sh” extension, for example, “my script. sh”. This extension indicates that the file is a shell script.

Writing the Script

Now that we have our script file ready, it’s time to write our commands. A shell script starts with a shebang (#!) followed by the path to the shell interpreter. For Bash, the shebang would be:


Copy code


Next, add your desired commands. These can be any valid shell commands or a combination of them. For example, let’s create a simple script that prints “Hello, World!” on the terminal:


Copy code

#!/bin/bash echo “Hello, World!”

Making the Script Executable

Before we can run our script, we need to make it executable. In Linux, we use the chmod command to change file permissions. To make our script executable, use the following command:


Copy code

chmod +x

Running the Script

With the script set as executable, we can now run it using the following command:


Copy code


The “./” before the script name tells the shell to look for the script in the current directory. If the script is located in a different directory, provide the full path to the script.

Passing Arguments to Shell Scripts

Shell scripts can accept arguments, allowing for more dynamic and versatile usage. To pass arguments to your script, you can use special variables within the script. For example, the variable $1 represents the first argument, $2 the second, and so on.

Let’s modify our “Hello, World!” script to accept a name as an argument and print a personalized greeting:


Copy code

#!/bin/bash echo “Hello, $1!”

Now, when you run the script with an argument, it will greet you with a personalized message:


Copy code

./ John

Redirecting Output

In shell scripting, you can redirect the output of commands to files or even to other commands. This can be helpful when you want to save the results of a script or use them as input for another operation.

To redirect the output to a file, use the “>” symbol. For example:


Copy code

./ John > greeting.txt

This will save the “Hello, John!” output to a file named “greeting.txt.”

DNS Secure Test: Secure Your Online Journey

Conditional Statements and Loops

Shell scripting becomes even more powerful with the use of conditional statements and loops. You can add decision-making logic and repetitive operations to your scripts.

Conditional Statements

Conditional statements, such as if, else, and elif (else if), allow your script to take different paths based on conditions. For example:


Copy code

#!/bin/bash if [ $1 -gt 5 ]; then echo “Number is greater than 5.” else echo “Number is not greater than 5.” fi


Loops enable you to perform repetitive tasks until a certain condition is met. The most commonly used loops are for and while. Here’s an example of a for loop:


Copy code

#!/bin/bash for i in {1..5}; do echo “Number: $i” done

This loop will print the numbers 1 to 5 on the terminal.

Tips for Efficient Shell Scripting

  • Use descriptive variable names to enhance script readability.
  • Comment your code to explain complex sections or provide context.
  • Test your script thoroughly before using it in a production environment.
  • Keep your scripts modular and well-organized for easier maintenance.


You’ve now mastered the art of running shell scripts in Linux. We covered the basics of creating, writing, and running shell scripts, as well as passing arguments, redirecting output, and using conditional statements and loops. With this newfound knowledge, you can automate tasks and streamline your workflow like never before.

RDP vs RDC: An In-Depth Examination


1. How do I run a .sh script in Linux?

Use the command ./ after giving it execute permissions with chmod +x

2. How can I run a shell script?

Make the script executable with chmod +x and then run it with ./

3. How do I run a shell script in the terminal?

Navigate to the directory of the script, grant execute permissions with chmod +x, and then run with ./

4. How to run a bash script?

Use bash or make it executable and run with ./

5. How to run a shell script in a Linux example?

For a script named “”: First, chmod +x, then run with ./

6. How to run the shell script in the Linux command line?

After granting execute permissions (chmod +x, run the script using ./

7. How to run a shell script in Ubuntu?

It’s the same as Linux. Use chmod +x followed by ./

8. How to run a shell script in Windows?

Windows doesn’t natively run shell scripts. Use a tool like Git Bash, Cygwin, or Windows Subsystem for Linux (WSL) to execute the script.

9. How to run a bash script in Linux?

Use the command bash or make the script executable with chmod +x and then run it using ./

10. How to run a sh file in a terminal on a Mac?

Use the Terminal to navigate to the script’s location and grant execute permissions with chmod +x, and then run it using ./

11. How to give execute permission to a shell script in Windows?

Native Windows doesn’t have a chmod equivalent. However, if you’re using a tool like Git Bash or WSL, you can use chmod +x to grant execute permissions.

Noor Khan

Noor Khan


My name is Noor, and I am a seasoned entrepreneur focused on the area of artificial intelligence. As a robotics and cyber security researcher, I love to share my knowledge with the community around me.

Other interesting articles

Automated vs Manual Penetration Testing

Automated vs Manual Penetration Testing

Pentesting is largely divided into two methodologies: Automated vs Manual Penetration Testing. Both have ...
8 Steps in Penetration Testing You Should Know

8 Steps in Penetration Testing You Should Know

Mastering the art of penetration testing has become a critical ability for security experts to combat cyber ...
Spear Phishing vs Whaling: What is the Difference

Spear Phishing vs Whaling: What is the Difference

Spear phishing is a particularly devious type of phishing assault in which the individual targeted plays a ...

Submit a Comment

Your email address will not be published. Required fields are marked *