Key Points
- To run shell scripts on Windows, you must enable WSL and Virtual Machine Platform, install a WSL distro, and use “Bash Script.sh” to execute it.
- Alternatively, use third-party tools like Cygwin, Cmder to run Linux commands on top of Windows OS.
If you have worked with Linux distributions and repositories, you would know what shell scripts are. To run shell scripts on top of a Windows computer can be challenging, as the process is not quite straightforward.
Shell scripts are a piece of code written in plain text but have the “.SH” file extension. These are usually used to automate tasks and execute Linux or Unix packages. These are written in Bash and start with “#! /bin/sh
“.
You do not need to install a Linux or Unix distribution alongside your Windows operating system to run a shell script. In this article, we’ll show you how to run a shell script on a Windows PC.
Table of Contents
What are Shell (.SH) Files
The Linux/Unix operating system uses shell or .SH script files to perform certain tasks. In comparison, it can be substituted by Windows batch files for the Windows operating systems, which are used to execute commands on a computer.
The Batch language is a simple and interpreted programming language, unlike C++ or C#. However, you do not have to translate a shell script file to batch to perform similar tasks on a Windows computer, as you do on a Linux PC.
There are different methods to run the same .SH file on a Windows computer as on a Linux system. Let us show you how to run a shell file on a Windows PC.
Run Shell Script (.SH) File on Windows
There are both native and third-party methods to run shell files on a Windows computer. You can use Cygwin, or a third-party tool of your choice, to run a shell script on Windows OS. Alternatively, you can install the Windows Subsystem for Linux feature on Windows and then execute the shell script file through it.
Below you’ll find the methods to run the file using both methods.
Run Shell Script File using Windows Subsystem for Linux (WSL)
This section has been divided into 3 steps to make the process easier to understand. You must do the following 3 things to run a shell script file in the given order:
Note: If you are performing these steps on a Virtual Machine, then you will need to enable Nested Virtualization.
Install WSL
Use the following steps to install WSL on your computer:
-
Press the Windows Key + R shortcut keys to open the Run Command box.
-
Type in “optionalfeatures” and press Enter.
-
Select “Windows Subsystem for Linux” and “Virtual Machine Platform,” and then click Ok.
The wizard will now install WSL.
-
When the installation is completed, click “Restart now.”
Once the computer restarts, it is time to install a Linux distro.
Install a Linux Distribution
Use the following steps to install a Linux distribution:
-
When it restarts, press Windows Key + R again to open Run.
-
Type in “cmd” and press CTRL + Shift + Enter to run Command Prompt as administrator.
-
Run the following command to obtain a list of available Linux distros:
wsl --list --online
Note down the name of the distro you want to install.
-
Use the following command to install the preferred distro while substituting its name:
wsl --install -d [DistributionName]
The distro will now begin to download and install.
-
Set up a username and password.
-
Restart the computer.
Now, the next step is to simply run the shell script file.
Run the Shell Script File
Follow the simple steps below to run the shell script file on a Windows PC:
-
Press the Windows Key + R again to open Run.
-
Type in “cmd” and press CTRL + Shift + Enter to run Command Prompt as administrator.
-
Use the CD cmdlet to change the directory to the location of the shell script file.
CD [PathToShellScriptFile]
-
Now, use the following command to run the shell script file while substituting [ShellFileName] with the complete and correct name of the shell file:
Bash [ShellFileName].sh
This is how you run a shell script (.SH) file on a Windows computer using WSL.
If this seems like a lengthy process for you, then you can also use the alternative method given below.
Run Shell Script File using Third-Party Tools
You can also run a shell script file on a Windows computer using a third-party tool. In the steps below, we have used Cygwin.
Follow these steps to run a .SH file on a Windows PC:
-
First, download and install Cygwin from their official website.
-
Once installed, run the Cygwin app.
-
In the Cygwin terminal, use the
CD
command to change the directory to the shell script file location.Note: In Cygwin, the syntax to change the directory is different from Command Prompt or PowerShell. Use the following command syntax and example in the image to change your directory in Cygwin.
CD /cygdrive/[driveLetter]/[Subfolder1]/[Subfolder2]
-
Now, make the shell script file executable through this command:
chmod +x [ShellFileName].sh
-
Now, use the following syntax to run the shell script file:
./[ShellFileName].sh
These are the two methods to run shell script files (.SH) on a Windows PC. Of course, there are many other third-party tools available that allow you to run Linux and Unix files on a Windows computer.
Frequently Asked Questions (FAQs)
How to create a shell script (.SH) file?
I you know what you want to write inside the shell script file, all you need to do is follow these simple rules:
– Create a new notepad file
– Start the script with “#! /bin/sh”
– Write the code below it
– Save the file with a “.sh” extension
– To run the script, type “bash [FileName].sh” after navigating to the folder.
Can I run shell scripts on Windows?
Yes, shell script files can be executed on a Windows computer using Windows Subsystem for Linux, or third-party tools like Cygwin, kiTTY, ConEmu, Cmder, etc.
Why use shell scripts on Windows?
Shell scripts are used to perform repetitive tasks to save time and resources. They are also used to automate tasks. With the WSL feature, Microsoft has opened doors to natively run shell scripts on Windows operating systems.