What are SH Files and How to Execute Them?

Script files contain commands that are executed by a certain program or scripting engine. These commands are executed without being compiled. Instructions are written in scripting languages for run time environment. There are many scripting languages with different purposes and environments. However, the one we are going to discuss in this article is a bash scripting language that is used to work in Linux. The files containing the commands or syntax of bash scripting language are also known as SH files or Shell Script files.

What are SH Files and How to Execute Them?

What are SH Files in Linux?

Script files are known to be created and saved in the bash language because the instructions it contains are written in that language and it comes with an extension of .sh. Any command that you want to normally run in Terminal can be put into a script file to execute them. If you have a sequence of commands that you want to execute frequently then you can put it into a script and just call the script.

It is not important for a script file to have an extension of .sh. Linux is not Windows, because the first line of code called ‘shebang‘ in the file will be enough to ensure it as a script file for the programs. It’s a common convention to use a .sh extension for shell scripts but it’s not a useful convention. Putting the extension for the advantage of noticing the file as a script is not much and it will also lose the flexibility.

SH Files in Linux

How to Create an SH File?

Just like other coded files created in the simple text editor, this can be done the same. All you need to know is the commands and syntax for the language to create one. However, the most important thing for the bash shell script file is that it starts with a shebang that is shown below:

Shebang in a script file

This will be added to the first line of code to make a bash script executable. You can also check the location of bash interpreter by the following command:

which bash
Which bash command in Terminal

Also, we need to use another command so that the system will recognize permission for this as an executable file. However, this might not be required in some cases:

chmod +x filename

Note: Filename can be any name that you give to your file.

Executing SH Files through Terminal in Linux

You can execute SH files if text commands are typed within the Terminal. The syntax of code in your SH file must be correct before executing it. We are just using sample code to demonstrate; how the script file works. You can have a different code that you are working on.

  1. Open any text editor that you prefer on your system.
  2. Now type the following sample code and save it with or without sh extension:
    echo Hello, appuals users!         #echo is used to display the line of text
    echo What is your name?            #Program will ask for input
    echo what                          #Here user need to give input
    echo Hello, $what!                 #Input will be printed with text
    Sample Script Code

    Note: Read comments to understand each line of sample code.

  3. Now open Terminal by pressing Ctrl + Alt + T keys altogether.
  4. There are several commands to run/execute the script file:
    Executing a script file
    sh appuals
    Executing a Script file using the sh command
    bash appuals
    Running a Script file using the bash command

    Note: Name of file can be anything. Here we name the script file as “appuals” without any extension. Also, make sure you are in the correct directory where the file is located.

  5. If the file is not executing then type the chmod command to make the system recognize the permission for this executable file. If the system already recognizes it as an executable file then skip this step:
    chmod +x appuals
    Using chmod command for permissions of a script file

Kevin Arrows

Kevin is a dynamic and self-motivated information technology professional, with a Thorough knowledge of all facets pertaining to network infrastructure design, implementation and administration. Superior record of delivering simultaneous large-scale mission critical projects on time and under budget.

Expert Tip

What are SH Files and How to Execute Them?

If the issue is with your Computer or a Laptop you should try using Restoro which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Restoro by clicking the Download button below.

Download Now

I'm not interested