How to Run Shell Script (.sh) Files in Linux

Shell scripts play an important role in the world of Linux. They help automate repetitive tasks and streamline complex procedures. This doesn’t just save users’ time—it significantly reduces the chance of human errors, resulting in more reliable outcomes and improved system stability.

In this blog post, we’ll explore three different methods to run shell script files in Linux. Note that when we talk about shell script files, we specifically refer to scripts written in the Bash scripting language. Let’s get started!

Prerequisites

To follow along with the examples in this blog post, you'll need access to the Linux operating system. For the purpose of this blog post, I'll be using Ubuntu as my Linux distribution of choice. Specifically, I will use KodeKloud’s Ubuntu playground, which lets you access a pre-installed Ubuntu operating system in just one click. Best of all, you won't need to go through the hassle of installing any additional software—everything you need is already set up and ready to use.

Note that the examples and commands in this blog post are meant to be executed in a Bash shell, which, by default, is one of the most common shell environments in Linux distributions.

How to Create a Shell Script

Before we explore different methods to run script files, it's essential that we have a script file to work with. So, let's create one named demo.sh. But where should we put this file?

You can put this file anywhere you want. However, a common location often used for custom script files is /usr/local/bin. The main reason behind this preference is that in many Linux distributions, this directory is typically included in the system’s command search path. As a result, scripts placed in this directory can be run from anywhere without needing to specify the full path to the script.

Before we proceed, let's confirm whether /usr/local/bin is included in the command search path. We can check this by examining the PATH environment variable, which contains a list of directories the system searches when trying to execute a command.

Run the following command:

echo $PATH

You should see an output like this:

As highlighted above, /usr/local/bin is listed in the PATH. Next, let's proceed with creating our script file in this directory.

To create a script file In Linux, you'll need a text editor. Ubuntu, for instance, comes with several pre-installed editors, such as "nano" and "vim". For the purpose of this tutorial, we'll use "nano".

To ensure that "nano" is available, check its version by running the following command:

nano --version

The output, as shown below, indicates the version of the "nano" editor installed.

Once confirmed, launch the "nano" editor and create our demo.sh script file inside /usr/local/bin, using the following command:

nano /usr/local/bin/demo.sh

This will open the "nano" editor, as shown below:

Now, let's add some content to our script. In the nano editor, type the following:

#!/bin/bash
echo "Hello World!"

The first line, #!/bin/bash, is known as a shebang (also called a hashbang). It tells the system that this script should use the bash interpreter to parse the rest of the file. The second line is a simple command that prints Hello, World! to the console.

After writing this script, you'll need to save and exit the "nano" editor. To do this, press Ctrl + O. You'll then be prompted with a message saying File Name to Write, simply press "Enter" to confirm the pre-filled file name. Once your file is saved, you can exit "nano" by pressing Ctrl + X.

We've now created a shell script file named demo.sh inside the /usr/local/bin directory. We’ll explore different ways to run this file in the subsequent sections.

3 Methods to Run Shell Script Files in Linux

Below, we’ll explore three different methods to run shell scripts in Linux.

#1 Running Shell Scripts Using the Bash Command

The bash command is typically used to start a new Bash shell session. However, if you provide it an argument, it treats that argument as the name of a script file to be run

So, to run the script file demo.sh, execute the following command:

bash demo.sh

This command starts a new "bash" process, which reads the commands in the file and executes them one after the other. After running the command above, you should see Hello World! printed on the terminal:

Note that when you run a script with "bash", the script doesn't need to have execute permissions. This is because you're asking "bash" to read the file and interpret it, not asking the operating system to execute the file as a program.

#2 Running Shell Scripts as Standalone Programs

Running shell scripts as standalone programs means that you treat the script in the same way you would any other executable program in your Linux system.

Normally, when you want to run a program in Linux, you type the name of the program at the command line and press enter. The system will then look for an executable file by that name in the directories listed in your PATH environment variable. If it finds an executable file, it runs that program.

You can do the same with shell scripts. But first, you must make the shell script executable. Then, you can run it just by typing its name at the command line, just like you would with any other program.

First, let's verify whether our script file demo.sh has the necessary execute permission.

To do so, cd into the /usr/local/bin folder and then run the ls -l command followed by the name of the script:

cd /usr/local/bin && ls -l demo.sh

The -l flag (which stands for "long format") gives a detailed listing of files and directories.

After running the command above, you'll see an output similar to this:

In this string, "-rw-r--r--" the -rw- indicates that the user (you) have read and write permissions on the file but not execute permission. The next two sets of "r--" indicate that the group and others have only read permission, not write or execute.

To add execute permission for the user, group, and others, we'll use the chmod command (which stands for "Change Mode") followed by +x (which stands for "add execute permission"), and then the name of our script file:

chmod +x demo.sh

After running the above command, run ls -l demo.sh again. You'll see that the permissions have changed, as shown in the output below:

The -rwx indicates that the user now has read, write, and execute permissions on demo.sh. The r-x for group and others means they now have both read and execute permissions.

Now that demo.sh has execute permissions, you're ready to run it as a standalone program. To do so, simply type the name of the script and hit enter:

demo.sh

The script will execute just like any other command-line program. It'll run in its own shell (a subshell), independent from your current shell.

After running the command above, you should see Hello World! printed on the terminal, as shown in the output below:

#3 Running Shell Scripts Using the Source Command

The two methods we discussed so far – running shell scripts as standalone programs and running shell scripts using the "bash" command – execute your script in a new shell instance (spawned in the background). This means any changes your script makes are confined to that new shell. Once the script completes, that new shell closes, and its changes do not affect your current shell.

However, there may be scenarios where you'd want your script to modify the environment of your current shell. Perhaps your script sets certain environment variables, changes the working directory, or defines functions that you'd like to use after the script finishes running. This is where the source command comes into play.

The source command, or its shorthand dot (.), runs your script in the current shell, not a new one. Therefore, any changes your script makes to the environment persist in the current shell after the script finishes.

To use the source command to run the demo.sh script, execute the following command:

source demo.sh

Or, you can use the shorthand:

. demo.sh

Running either of these commands tells your current shell to read demo.sh and execute its commands right there in the current shell. You should see Hello World! printed on the terminal as shown below:

Before we conclude, there is an important piece of information to keep in mind. In the previous three methods, you might have noticed we didn't specify the path for the demo.sh file. Why?

That's because the shell automatically searches for executables within the directories listed in the PATH variable. Since we've placed our demo.sh file in the /usr/local/bin directory, which is included in the PATH variable, the shell can find and execute it without needing the full path.

What if you place your file in a directory not listed in the PATH variable, and you choose not to move it? In such cases, you must provide the file's full path, either absolute or relative, when executing it.

Conclusion

In this blog post, we explored three different methods for executing shell script files in Linux: running scripts using the bash command, running scripts as standalone programs, and running scripts using the source command. As you continue your journey with bash scripting, you'll find that this flexibility is invaluable, enabling you to adopt the method that best suits your specific use case.

Looking to build a solid foundation in shell scripting? Check out this course from KodeKloud:

  • Shell Scripts for Beginners: In this course, you'll dive into the practical world of Linux shell scripting. Regardless of your programming experience, you'll master fundamental scripting concepts such as variables, loops, and control logic. Throughout the course, you'll get plenty of hands-on experience using our comprehensive labs. Not only that, you'll also receive immediate feedback on your scripts, which will help you improve and refine them.