Files with the .sh
extension, i.e., shell scripts, are designed to be run by Unix shells such as Bourne or GNU Bash. If you attempt to run shell scripts in Windows, it won’t work as the commands won’t be recognized by default.
Instead, you’ll have to introduce a compatibility layer to be able to run such scripts in Windows. There are various ways to do this, with the popular ones being Windows Subsystem for Linux, Cygwin, and Git Bash.
In this article, we’ve listed all the necessary steps to run Shell Scripts in Windows using these methods.
Windows Subsystem For Linux
The first method is to install a Linux distro through the Windows Subsystem for Linux feature. Cygwin and Git are convenient, but users often have difficulty running complex shell scripts and Linux applications with them. As such, WSL is the recommended option. Here are the necessary steps for this:
- Press Win + R, type
optionalfeatures, and press Enter.
- Enable the Windows Subsystem for Linux and Virtual Machine Platform features, and press OK.
- After the changes are applied, restart the computer.
- When booting, press the BIOS key (Fn keys or Del).
- Go to the Advanced CPU Configuration or similar section and look for the virtualization option. It will be named something like AMD-V, SVM, VT-d, or VT-x.
- Enable the virtualization option if it isn’t already so, and press F10 to save the changes and exit.
- After booting, download and install the WSL2 Linux Kernel Update Package.
- Press Win + R, type
powershell
, and press Enter. - Execute the following command to set WSL 2 as the default version when installing new Linux distros:
wsl --set-default-version 2
- Open the WSL Store, select your preferred distro, and press Get to download it. If you’re unable to install WSL Linux Distros from the Microsoft Store, you can directly download and install them using these links provided by Microsoft.
- After it’s downloaded, press Open.
- On the first launch, the installation will take a couple of minutes.
- After the installation completes, enter a new UNIX username and password.
- Before anything else, it’s good practice to update and upgrade your packages. On Debian-based distros, you can do this with the following command:
sudo apt update && sudo apt upgrade
- Navigate to the location of the script file.
- Hold Shift, right-click, and select Open Linux Shell here.
- Enter
sh scriptname.sh
to run the shell script.
Use Cygwin
Cygwin is a run time environment used to run code designed for Unix environments on Windows. Here’s how you can run shell scripts in Windows using Cygwin:
- Download and launch the Cygwin installer.
- Select Install from Internet as the download source.
- Specify the root directory to install Cygwin to. Also, select whether Cygwin should be available for all users or just the current one.
- Select the default options for local package directory and internet connection.
- For the download site, choose a mirror geographically close to you if possible. Otherwise, any mirror will do fine. The difference in download speed shouldn’t be anything major.
- Don’t select any packages for the time being. Just press Next and follow the on-screen instructions to complete the installation.
- Open the Cygwin64 Terminal and navigate to the location where the shell scripts are stored.
cd <scriptpath>
- Use the following command to run the shell script:
sh scriptname.sh
Use Git Bash
During its installation, Git optionally adds some UNIX tools to the PATH in Windows, with sh.exe
being one of them. Here’s how you can install Git and run shell scripts using Git bash:
- Download and install Git.
- During the installation, make sure to include Git bash and add Git to the PATH variable.
- After the installation is complete, navigate to the folder with the shell script.
- Right-click on an empty area and select Show More Options > Git bash here.
- Once again, use the
sh scriptname.sh
command to run the shell script.