How to run .sh file on Linux, Mac, Windows, & Ubuntu terminal

By John Adebimitan

There are ways to communicate with a computer’s operating system and carry out actions like managing files and folders, running or terminating programs, monitoring and configuring operating systems, and running batch processing using a shell. A shell is a program a human user and other programs use to interact with a computer’s operating system.

Operating system shells generally use a GUI (graphical user interface) or a CLI (command-line interface), depending on the operation and the computer’s role.

Graphic user interface shells are easier to use, especially for beginners. Still, they have a limit, so command-line interface shells are included in operating systems. When using a CLI shell, you put your commands in a shell file so that when you want the shell to execute the file, it’ll run all the commands in that order.

Read this article to know more about .sh files and running .sh files on your Windows, Mac, or Ubuntu OS.

What is running a script?

A script is a set of commands within a shell file executed by a program or an operating system without being compiled. In computer geek terminology, a compiler is a program that converts or translates a programming language to another programming language, usually a lower one.

When writing a script, you combine a set of sequential commands stored in the file to be executed whenever you want. Some examples of common scripting languages are Bash, PowerShell, Python, Perl, etc.

Everything you need to know on how to run .sh files

A .sh file or a shell script is a Bash shell file with a chain of CLI shell commands executed whenever you instruct the shell program to execute the file. Bash Shell Script is a kind of Unix shell.

A .sh file makes it easy for you to run commands and simplify repetitive, time-consuming tasks. To create your shell commands, you will need to know the syntax of the particular scripting language you are writing in, in this case, Bash. For example, a Python syntax is different from a PowerShell syntax.

How to run .sh file in Linux and Unix

There are two main ways to run a .sh file script on Linux.

You can try:

  • Launch the terminal on your Linux or Unix PC.
  • Use a text editor to create a new script file. Script files end with a “.sh” extension.
  • Use “nano script-name-here. sh” to write the script file.
  • To execute permission on your script, use the chmod command: “chmod +x”.
  • You can run your script using three commands. You can use “./”, “sh” or “bash”.

You can also try running it as a root user. For example, sometimes, you would need root access when trying to install an application. Root access gives you the necessary permissions to install the application and make system-level changes. On Linux and Unix-like systems, root access is disabled by default. To run as a root user:

  • Type “sudo bash”.
  • Next, input your password.

Another option is to become a superuser by using the su command. Type “su –. “ Next, type in your root user password, then run your script like above.

Alternatively, you can use the chmod command to run it. Run the chmod command first, chmod +x then run your .sh file like this:

How to write a script in Linux and Unix

You can write a shell script using a text editor like Atom or Visual Studio Code. To write a shell script in Linux, you would have to open a new file in your text editor, then give the shell permission to execute your script. Next, write the programming in the shell script and put the script in the location the shell can find it. Here’s a step-by-step example:

  • Create a file using your editor and save it with the .sh extension.
  • Start writing the script with #!/bin/sh
  • Write some codes.
  • Save the script as
  • To execute the script, type bash

How to run a .sh file in Vscode?

Visual Studio Code is a very versatile tool for running and editing code. It would help if you made small changes to your VS code settings to run a .sh file. If you’re using a Windows PC, VScode automatically uses Windows PowerShell to run shell scripts. To get it to run .sh files, you need to get the Bash extension. You can run a .sh file in simple steps:

  • Download and install the Git extension from here.
  • Run your Visual Studio Code and hold Ctrl + ` to open the terminal.
  • Press Ctrl + Shift + P to open the command palette.
  • In the dialogue box, type Select Default Profile.
  • Select Git Bash from the options and click on the + icon in the terminal. It will take a little while to load Git Bash.
  • When it has loaded, you can switch from the PowerShell or Bash terminal in the terminal dropdown.
  • To run the .sh file, go to the file’s location and open it with Visual Studio Code.

How to run .sh file on Mac

You can run a shell script on your Mac using the sudo command that gives you root access.

  • On your Mac, press Cmd + Space, and type Terminal in the search box. Press Enter to open it.
  • In the terminal, type “sudo sh
  • Next, open your Finder to locate the shell script. Drag and drop the shell script next to sudo sh. This would give the terminal the path to the script file.
  • A prompt for the administrator password will show. Input it and press Enter.
  • The script should run perfectly now.

You can also use third-party software to run shell scripts, especially if you’re running them frequently. You can use various third-party software to edit and run shell scripts on the go. For example, you can download a simple, versatile text editor like Visual Studio Code. Here’s how to go about it:

  • Install your text editor and run it.
  • Next, open your Finder and look for the shell script—Right-click on the file to open it with VSCode.
  • When the file opens in Visual Studio, click on Tools in the top menu, then click on Build in the dropdown.
  • Visual Studio would ask for permissions. Click on Allow when it shows. In the terminal below, you should see your results in real-time. You can directly edit and save shell scripts in your Visual Studio code.

How to run a script in Windows 10?

You can run scripts directly from your command prompt using Windows Script Host. Windows Script Host has two command-line interfaces; WScript and CScript. WScript and CScript work the same way, the only difference being that WScript displays windowed output while CScript shows the output in the command window in the script was written.

In addition, CScript.exe and WScript.exe provide command-line options to change script properties. To run a script using WScript:

Right-click on the Start menu and select Run from the options. In the Open search field, type the full path of the script and select OK. Or type WScript followed by the full name of the script and the script path.

When you double-click on a script file that hasn’t been opened with WScript.exe, the Open with option will show, and you can choose the default application you want to use to open the file and subsequent script files.

Alternatively, you could also run scripts in CScript. Since WScript is the default command line of Windows Script Host, you can change from WScript to CScript by typing this command in the command prompt terminal: cscript //h:cscript and alternatively change from CScript to WScript by typing the command: wscript //h:cscript. To run with CScript:

  • Open your Start menu and search for the command prompt in the search box. When it shows, run it as administrator.
  • Type in this command in the terminal: cscript [host options…] [script name] [script options and parameters]

Read also: How to Create an INI File on Windows, Mac, and Android

How to run bash script in terminal

To run a bash script, first, make sure it has to execute permissions using the chmod command. In the terminal type: “chmod +x”. To run the bash script, type this command in the terminal: “bash”.

Why do most shell scripts contain #! /bin/bash at the beginning of the shell scripts

As previously explained, a shell is an interface for humans and other programs to interact with an operating system. Every distribution has a unique default shell. For example, Windows PowerShell is different from a Bash shell, and so on.

Bash is the default shell most systems have, so if you are working on a system with a different shell, the scripts might not run if they were explicitly intended for Bash.

The #! /bin/bash code in the first line of your script tells your OS to execute the script using the Bash shell as the interpreter. The #! /bin/bash is also referred to as the “hash-bang,” “sha-bang,” or “she-bang.”

About The Author