Skip to content
Zepe

How to Run a Shell Script (.sh) on Windows

By Updated 6 min read

The short answer

Run wsl --install in an administrator terminal, restart, then run bash script.sh inside WSL. For something lighter, right-click the folder and choose Git Bash Here. Command Prompt and PowerShell cannot run .sh files directly.

How to Run a Shell Script (.sh) on Windows — article cover

Four things worth knowing

  • A .sh file is a Unix shell script; Windows has no native interpreter for it, so you need WSL, Git Bash or Cygwin.
  • WSL 2 runs a real Linux kernel and is the right choice when the script installs packages or expects a full Linux system.
  • Git Bash is enough for simple scripts and is already installed on most developer machines.
  • $'\r': command not found means Windows line endings — convert with dos2unix or set your editor to LF.

A .sh file is a script written for a Unix shell, normally bash. Windows has no interpreter for it built in, so double-clicking does nothing useful and running it from Command Prompt gives you a syntax error that looks alarming but simply means Windows has no idea what it is reading. What you need is something providing a bash environment, and there are three realistic options with genuinely different trade-offs.

Comparison table showing that WSL provides a full Linux environment with a package manager, while Git Bash provides bash and core utilities only, with different levels of Windows filesystem integration.
WSL, Git Bash and Cygwin compared. Git Bash is enough for a simple script. Anything that installs packages or expects real Linux paths needs WSL.

WSL: the complete answer

The Windows Subsystem for Linux runs a real Linux distribution alongside Windows, and it is remarkably good. WSL 2 uses an actual Linux kernel in a lightweight managed virtual machine: it starts in about a second and needs no separate window. If your script does anything beyond shuffling files about, this is the option you want.

Install WSL and run a script

  1. Open an administrator terminal — Win + X, then A.

    See opening a terminal in Windows for the alternatives.

  2. Run wsl --install and let it finish.

    This enables the required Windows features and installs Ubuntu. wsl --list --online shows the other distributions available.

  3. Restart when prompted.

    The virtual machine platform needs a reboot. The installation completes on next boot.

  4. Set a Linux username and password when the distribution first starts.

    Unrelated to your Windows account. The password is what sudo asks for.

  5. Navigate to your script and run it: cd /mnt/c/Users/you/scripts then bash setup.sh.

    Your Windows drives are mounted under /mnt, so C: is /mnt/c.

Git Bash: probably already installed

Git for Windows quietly bundles a minimal bash environment along with the core Unix utilities: ls, grep, sed, awk, curl, ssh. For a script that copies files, runs a build tool or calls an API, that is entirely enough, it starts instantly, and there is a decent chance you already have it installed without realising.

  1. Install Git for Windows from git-scm.com if you do not already have it.
  2. Right-click the folder containing the script and choose Git Bash Here — on Windows 11 you may need Show more options first.
  3. Run bash script.sh, or ./script.sh if the script has a shebang line.
  4. Windows paths work with forward slashes: /c/Users/you/scripts is C:\Users\you\scripts.

Where Git Bash falls short is anything requiring a package manager or a real Linux binary. A script starting with apt-get install will fail, and no amount of configuration will change that: it is not a Linux system, it is a set of Unix tools compiled for Windows.

The line-ending error, which you will hit eventually

This is far and away the most common problem with .sh files on Windows, and the error message does absolutely nothing to explain itself, which is why it wastes so much of people's time:

  • $'\r': command not found
  • syntax error near unexpected token $'{\r''
  • /bin/bash^M: bad interpreter: No such file or directory

All three mean the same thing. Windows ends lines with carriage return plus line feed (\r\n); Unix uses line feed alone (\n). Bash reads the stray carriage return as part of the command, so bash\r is not an interpreter it recognises and every line ends with an invisible extra character.

Fix Windows line endings in a script

  1. In WSL, install and run the converter: sudo apt install dos2unix then dos2unix script.sh.

    It reports how many lines it converted, which confirms the diagnosis.

  2. Without dos2unix, use sed: sed -i 's/\r$//' script.sh.

    Works in Git Bash too, where dos2unix may not be available.

  3. Set your editor to use LF. In VS Code, click CRLF in the status bar and choose LF.

    This prevents the problem recurring the next time you edit the file.

  4. If Git is converting them, set git config --global core.autocrlf input.

    Git for Windows defaults to true, which rewrites LF to CRLF on checkout — a frequent and invisible source of this error.

Other errors worth recognising

Common errors when running .sh files on Windows
ErrorCauseFix
'.' is not recognized...Running it in Command PromptUse WSL or Git Bash
Permission deniedScript is not marked executablechmod +x script.sh, or run bash script.sh
bad interpreter: ^MWindows line endingsdos2unix script.sh
command not found for aptGit Bash is not a Linux distroUse WSL
No such file or directory on a real pathWindows path used in WSLUse /mnt/c/... not C:\...
Script runs but does nothingMissing shebang lineAdd #!/bin/bash as line 1
Common errors when running .sh files on Windows

Should you convert it to PowerShell instead?

Sometimes, and it is a question worth actually asking rather than assuming. If the script is short, moves some files around and calls a couple of tools, rewriting it in PowerShell removes the whole dependency and runs natively. No WSL, no Git Bash, nothing to install on the next machine you touch.

If the script is long, calls Linux-specific utilities, or came from somewhere you will need to update it from, translate nothing and use WSL. Maintaining a divergent Windows port of someone else's script is a worse job than installing WSL once.

If a script leaves something running and holding a port after it exits, the guide on killing a process using a port covers tracing and ending it. For a script that hangs entirely, force quitting covers the escalation order.

Common questions

Can I run a .sh file in Command Prompt?

No. Command Prompt and PowerShell have no bash interpreter, so a .sh file produces a syntax error rather than running. You need WSL, Git Bash or Cygwin. WSL is the most complete option and installs with a single command.

What does $'\r': command not found mean?

The script has Windows line endings (CRLF) where bash expects Unix ones (LF). The carriage return is being read as part of each command. Fix it with dos2unix script.sh, or sed -i 's/\r$//' script.sh. If it keeps recurring, Git's autocrlf setting is rewriting the file on checkout.

Is Git Bash enough, or do I need WSL?

Git Bash is enough for scripts that manipulate files, run build tools or call APIs. You need WSL for anything that uses a package manager, runs Linux binaries, or expects a real Linux filesystem: a script starting with apt-get install will never work in Git Bash.

How do I access my Windows files from WSL?

Your drives are mounted under /mnt, so C:\Users\you\Documents is /mnt/c/Users/you/Documents. Note that working across that boundary is slow; for active projects keep the files inside the Linux filesystem and reach them from Windows at \\wsl$\Ubuntu\home\you.

Sources

Each source is listed with the specific claim it supports.

  1. Install WSL Microsoft Learn

    Supports: The `wsl --install` command, the administrator and restart requirements, and that Ubuntu is the default distribution.

  2. Working across Windows and Linux file systems Microsoft Learn

    Supports: That Windows drives are mounted under /mnt, that \\wsl$ reaches Linux files from Windows, and that cross-filesystem work carries a performance penalty.

  3. Git for Windows downloads Git project

    Supports: That Git for Windows bundles a bash environment and Unix utilities, and provides the Git Bash Here context-menu entry.

About the author

Writes every guide on Zepe, and rewrites them when Windows changes. Every command here is run before it is published, and every claim is traced back to a primary source, listed above.

More about RobertReport a correction

All command line guides →