How to Execute .sh Files

How to Execute .sh Files

Overview

This short article will explain what is a ".sh" file and how to execute files with the extension ".sh".

What is a .sh File

A file with the ".sh" extension is a shell file or program that should be run in a command line interface short for CLI, ideally in Unix or Linux's bourne shell. The "terminal" program must be opened to execute ".sh" files in Unix or Linux. ".sh" files can also be executed in Windows with Command Prompt or Windows Powershell and macOS with macOS Catalina. Every operating system has an icon similar to the one down below to symbolize their CLI where ".sh" files can be run.

Why Run ".sh" Files

Running ".sh" files or scripts is efficient to automate processes on any computer or machine. If the same or similar tasks are being done every day, it is advised to create a script to command the computer or machine to run those tasks at a scheduled time to save a user their time from constantly doing the same tasks.

How to Execute .sh Files

Many commands can be written in a ".sh" file. At the top of the script, always include the shebang phrase "#!/bin/bash" to signify the file is a shell script. Next, include the keyword "echo" with a string afterward written in a similar format:

#!/bin/bash 
echo "Write something here"

By using "echo", strings or variables can be executed. For further information on reading, writing, and declaring other commands in bash, read the official documentation: https://www.gnu.org/software/bash/manual/bash.html (1)

Executing .sh files can be done by performing the following command:

bash "name_of_file".sh
  • Remember, the file must have a ".sh extension

  • If there is an error executing ".sh" files on Windows or any operating system, install Git Bash, which can be found here: https://git-scm.com/downloads (2)

Below shows Visual Studio Code running the bash file by using git bash:

References