How to run .sh on Windows Command Prompt?

Learn how to run .sh on windows command prompt? with practical examples, diagrams, and best practices. Covers windows, bash, command-line development techniques with visual explanations.

Running .sh Scripts on Windows Command Prompt

Hero image for How to run .sh on Windows Command Prompt?

Learn how to execute Bash (.sh) scripts directly from the Windows Command Prompt using various methods, including WSL, Git Bash, and third-party tools.

While Windows primarily uses batch files (.bat) or PowerShell scripts (.ps1) for command-line automation, the increasing prevalence of Linux-based development and tools often requires running Bash (.sh) scripts. This article will guide you through the most common and effective methods to execute .sh files directly from your Windows Command Prompt (CMD), enabling a more seamless cross-platform workflow.

Method 1: Windows Subsystem for Linux (WSL)

Windows Subsystem for Linux (WSL) is Microsoft's official solution for running a GNU/Linux environment directly on Windows, without the overhead of a traditional virtual machine. It provides a compatibility layer for running Linux binary executables natively. This is the recommended approach for most modern Windows users.

1. Enable WSL

Open PowerShell as an administrator and run the command: wsl --install. This will enable the necessary features and install a default Linux distribution (usually Ubuntu). You may need to restart your computer.

2. Install a Linux Distribution (if not default)

If you prefer a different distribution or need to install one manually, you can list available distributions with wsl --list --online and install one with wsl --install -d <DistributionName>.

3. Access your script from WSL

Once WSL is set up, you can access your Windows files from within the Linux environment. Windows drives are mounted under /mnt/. For example, your C: drive is accessible at /mnt/c/.

4. Run the .sh script

Navigate to the directory containing your .sh script within the WSL terminal and execute it using bash your_script.sh or ./your_script.sh (after making it executable with chmod +x your_script.sh).

# Example: Running a script located on your Windows C: drive
cd /mnt/c/Users/YourUser/Documents/Scripts
bash my_script.sh

Executing a Bash script via WSL from the Windows C: drive

Method 2: Git Bash

Git Bash is a command-line interface that provides a Bash emulation environment on Windows. It's primarily used for interacting with Git repositories but also offers a convenient way to run many common Unix commands, including Bash scripts. If you have Git for Windows installed, you already have Git Bash.

1. Install Git for Windows

If you don't have it, download and install Git for Windows from the official website. During installation, ensure that the option to 'Use Git from the Windows Command Prompt' or 'Git and optional Unix tools from the Command Prompt' is selected. This adds Git Bash to your system's PATH.

2. Open Command Prompt

Launch your regular Windows Command Prompt (CMD).

3. Run the .sh script

Navigate to the directory where your .sh script is located using standard CMD commands. Then, execute the script by calling bash followed by the script name: bash your_script.sh.

cd C:\Users\YourUser\Documents\Scripts
bash my_script.sh

Running a Bash script using Git Bash from Windows CMD

Method 3: Cygwin

Cygwin is a large collection of GNU and Open Source tools which provide functionality similar to a Linux distribution on Windows. It provides a DLL (cygwin1.dll) which acts as a Linux API emulation layer, allowing many Linux programs to be recompiled and run on Windows. While powerful, it's generally more involved to set up than Git Bash or WSL.

1. Install Cygwin

Download the Cygwin setup-x86_64.exe installer from the official Cygwin website. Run the installer and select the packages you need. Ensure bash is selected, and consider adding other utilities your script might depend on.

2. Add Cygwin to PATH

After installation, you'll need to add the Cygwin bin directory (e.g., C:\cygwin64\bin) to your Windows system's PATH environment variable. This allows you to call Cygwin commands from CMD.

3. Run the .sh script

Open Command Prompt, navigate to your script's directory, and execute it using bash your_script.sh.

flowchart TD
    A[User wants to run .sh script on Windows CMD]
    A --> B{Is WSL installed and configured?}
    B -- Yes --> C[Use `wsl bash /mnt/c/path/to/script.sh`]
    B -- No --> D{Is Git Bash installed and in PATH?}
    D -- Yes --> E[Use `bash path\to\script.sh`]
    D -- No --> F{Is Cygwin installed and in PATH?}
    F -- Yes --> G[Use `bash path\to\script.sh`]
    F -- No --> H[Install WSL or Git Bash (Recommended)]
    C --> I[Script Executed]
    E --> I
    G --> I
    H --> J[Choose a method and retry]

Decision flow for running .sh scripts on Windows CMD