A sh script in Linux refers to a shell script written in the Bourne shell language (sh), one of Unix-like systems’ most commonly used shell scripting languages. Shell scripts are plain text files containing a series of commands that the shell interprets and executes. These scripts are used to automate tasks, execute a sequence of commands, and perform system administration tasks in Linux.
The importance of sh scripts in Linux lies in their versatility and ability to automate complex tasks. They are powerful tools for system administrators, developers, and users. With sh scripts, one can automate repetitive tasks, such as backups, file manipulation, system configuration, and software installations.
Keeping their importance in view, this post will enlist all the possible methods to run the sh script in Linux. As a reference, the commands/practical implementation will be performed on Ubuntu 22.04.
How to Run a sh Script in Linux?
Usually, two possible methods exist to run a sh script in Linux. The first is to make it executable and then run it using its path, and the second is to use the bash keyword and run it. We will look into both methods one by one with an example.
Prerequisites: Create a sh Script
Before getting into details, you must have the script to practice the methods in this guide. For instance, if we want to create/edit the script, we can use the nano editor as follows:
$ nano genie.sh
The above command will create a sh script and open it up in editing mode. We have written the following content in the script:
The first line contains the bash shebang, asking the system to interpret it using the bash. After that, there are three echo statements.
Let’s dig into the methods.
Method 1: Use the bash Keyword to Run the sh Script
Open the terminal and locate your script; if it is in your current working directory, then you can run the script by using the following command:
$ bash genie.sh
Moreover, if the script does not exist in your current directory, then you have to use the complete path/relative path to the script with the script. The syntax, in this case, would be:
$ bash /path/to/file.sh
Method 2: Make the Script Executable to Run the sh Script
In this method, we must make the script executable before running it. Before that, why do we want to make the script executable? Can we run it without making it executable (except method 1)? Let’s execute it first before making it executable:
The “Permission denied” error shows that the script needs to be executable. Let’s start the process:
Step 1: Make the Script Executable
Open the terminal and use the ls command to check whether the script is currently executable or not:
$ ls -l
Right now, the script name is in white color, showing that it is not executable. Let’s make it executable, as we did here for the script named “genie.sh”:
$ chmod a+x genie.sh
Step 2: Run the Script
Now, run the script using its path and name as the following command in our case:
The script has been executed, and the content is displayed on the terminal.
In Linux, a sh script can be run by making it executable and then running it using its absolute path, i.e., “./genie.sh”. The second method uses the bash keyword with the absolute path, i.e., bash genie.sh. You do not need to make the script executable when using the bash keyword.
Both methods are convenient to follow. However, it is recommended to use the execution method as it is efficient, and the executable script can also be used for other processing in the Linux system. You have learned both ways to run a sh script in Linux.
Your suggestions and comments are valuable, do comment in case of any suggestions or questions. For more posts on Linux, visit Linux Genie and subscribe to our newsletter.