Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Facebook Twitter Instagram
Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Tech News Today
Home»Windows»How To Run Sh. Or Shell Script In Windows

How To Run Sh. Or Shell Script In Windows

Anup ThapaBy Anup ThapaAugust 17, 2022
run shell script in windows

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:

  1. Press Win + R, type optionalfeatures, and press Enter. 
  2. Enable the Windows Subsystem for Linux and Virtual Machine Platform features, and press OK.
    windows subsystem for linux virtual machine platform
  3. After the changes are applied, restart the computer.
  4. When booting, press the BIOS key (Fn keys or Del).
  5. 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.
  6. Enable the virtualization option if it isn’t already so, and press F10 to save the changes and exit.
    gigabyte-svm
  7. After booting, download and install the WSL2 Linux Kernel Update Package.
  8. Press Win + R, type powershell, and press Enter.
  9. Execute the following command to set WSL 2 as the default version when installing new Linux distros:
    wsl --set-default-version 2
  10. 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.
    get-debian-microsoft-store
  11. After it’s downloaded, press Open.
  12. On the first launch, the installation will take a couple of minutes.
  13. After the installation completes, enter a new UNIX username and password.
    wsl-debian-installation
  14. 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
    wsl-debian-sudo-apt-update
  15. Navigate to the location of the script file.
  16. Hold Shift, right-click, and select Open Linux Shell here.
  17. Enter sh scriptname.sh to run the shell script.
    wsl-shell-script-in-windows
Note: For common WSL issues encountered both during and after the installation, we recommend referring to this troubleshooting guide from Microsoft.

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:

  1. Download and launch the Cygwin installer.
  2. Select Install from Internet as the download source.
  3. Specify the root directory to install Cygwin to. Also, select whether Cygwin should be available for all users or just the current one.
  4. Select the default options for local package directory and internet connection.
  5. 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.
  6. Don’t select any packages for the time being. Just press Next and follow the on-screen instructions to complete the installation.
    cygwin-installation-confirm-changes
  7. Open the Cygwin64 Terminal and navigate to the location where the shell scripts are stored.
    cd <scriptpath>
  8. Use the following command to run the shell script:
    sh scriptname.sh
    cygwin-shell-script-in-windows

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:

  1. Download and install Git.
  2. During the installation, make sure to include Git bash and add Git to the PATH variable.
    git-bash-windows-integration
  3. After the installation is complete, navigate to the folder with the shell script.
  4. Right-click on an empty area and select Show More Options > Git bash here.
  5. Once again, use the sh scriptname.sh command to run the shell script.
    git-bash-shell-script-in-windows
how-to
Anup Thapa
  • LinkedIn

Anup Thapa is a tech writer at TechNewsToday. He mostly writes informative articles, tutorials, and troubleshooting guides related to Windows systems, networking, and computer hardware. Anup has been writing professionally for almost 5 years, and tinkering with PCs for much longer. His love for all things tech started when he got his first PC over 15 years ago. It was a Pentium IV system running Windows XP on a single 256 MB stick. He spent his formative years glued to this PC, troubleshooting any hardware or software problems he encountered by himself. Professionally, Anup has had brief forays into a variety of fields like coding, hardware installation, writing, etc. In doing so, he's worked with people of different backgrounds and skill levels, from average joes to industry leaders and experts. This has given him not just a versatile skillset, but also a unique perspective for writing that enables him to concisely communicate complex information and solve his reader's problems efficiently. You can contact him at anup@technewstoday.com

Related Posts

firmware vs software

Firmware Vs Software – What’s the Difference?

March 8, 2023
update sound drivers

How to Update Sound Drivers on Windows

March 7, 2023
nvidia-users-account-is-locked

Fixed: NVIDIA Users Account is Locked

March 7, 2023
windows-failed-to-start

Fixed: Windows Failed to Start A Recent Hardware or Software Change Might Be The Cause

March 7, 2023
computer-wont-shut-down

Computer Won’t Shut Down? Here’re 8 Ways to Fix It

March 5, 2023
amd-software-not-opening

How to Fix AMD Software Not Opening

March 3, 2023
Add A Comment

Leave A Reply Cancel Reply

Latest Posts
firmware vs software

Firmware Vs Software – What’s the Difference?

March 8, 2023
keyboard sizes

All the Types of Keyboard Sizes Explained

March 8, 2023
Memory-Compression

What is Memory Compression in Windows? Should You Enable or Disable It

March 1, 2023
You may also like
keyboard is double typing

Keyboard is Double Typing? Here’re 6 Ways to Fix it

March 9, 2023
enlarge-hp-printer

How to Enlarge Copy on HP Printer

March 9, 2023
reset msi bios

How to Reset MSI BIOS

March 9, 2023
Recommended
Cookie Clicker Garden Guide

Cookie Clicker Garden Guide to Unlocking Every Seed

September 26, 2021
monitor no signal

Computer Turns On But Monitor Says No Signal (9 Ways To Fix)

November 10, 2022
Facebook Twitter Pinterest
  • Home
  • About Us
  • Our Team
  • Editorial Guidelines
  • Privacy Policy
  • Affiliate Disclosure
© 2023 TechNewsToday, editor@technewstoday.com | Tech Central Pvt. Ltd.

Type above and press Enter to search. Press Esc to cancel.