How to Run Shell Script in Windows
Introduction
Running a shell script on a Windows machine might seem tricky at first, but several reliable methods exist that let you execute Bash‑style scripts directly from the Windows environment. On top of that, whether you prefer the lightweight Windows Subsystem for Linux (WSL), the familiar Git Bash, the feature‑rich Cygwin, or even PowerShell, this guide will walk you through each approach step by step. By the end, you’ll know exactly how to prepare, transfer, and execute a shell script on Windows without losing the script’s original functionality.
Prerequisites
Before you can run any script, you need to set up the appropriate environment. The most common prerequisites are:
- Administrator rights on your Windows PC.
- Internet connection for downloading required tools.
- A text editor (Notepad, VS Code, Sublime, etc.) to edit the script.
The three main environments you’ll encounter are:
- WSL – a full Linux kernel running alongside Windows.
- Git Bash – a minimal Bash emulator bundled with Git for Windows.
- Cygwin – a richer POSIX compatibility layer.
Each method has its own installation steps, which we’ll cover in dedicated sections Simple, but easy to overlook..
Method 1: Using Windows Subsystem for Linux (WSL)
Enable WSL
- Open PowerShell as Administrator.
- Run the command:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart. - Restart your computer when prompted.
Install a Linux Distribution
- Open the Microsoft Store.
- Search for a distribution such as Ubuntu, Debian, or Kali Linux.
- Click Get and wait for the installation to finish.
Set WSL as the Default (optional)
If you want every new terminal to start in WSL, run:
wsl --set-default-version 2
Run Your Shell Script
- Launch the newly installed distro from the Start menu (e.g., Ubuntu).
- Update the package list:
sudo apt update && sudo apt upgrade -y. - Transfer your script to the Linux file system. You can copy it from Windows using the
\\wsl$network path or simply create the file inside the distro with a text editor. - Make the script executable:
chmod +x /path/to/your_script.sh
- Execute the script by typing its name or full path:
/path/to/your_script.sh
Tip: Ensure the script starts with a shebang (#!/bin/bash) so the system knows which interpreter to use.
Method 2: Using Git Bash
Install Git for Windows
- Download the latest version from the official Git website.
- Run the installer, accepting the default options.
- During installation, make sure to select “Git Bash” in the start menu folder.
Prepare the Script
- Place your
.shfile in any folder accessible from Git Bash (e.g.,C:\Users\YourName\scripts). - Open Git Bash.
- manage to the script’s directory:
cd /c/Users/YourName/scripts
- Grant execute permission:
chmod +x your_script.sh
- Run the script:
./your_script.sh
Note: Git Bash automatically translates Windows paths (e.g., C:\) to Unix‑style paths (/c/), so you don’t need to worry about path compatibility.
Method 3: Using Cygwin
Install Cygwin
- Download the Cygwin setup program from the official site.
- Run the installer and choose the packages you need (at minimum, bash, coreutils, gcc, and python).
- Finish the installation and launch Cygwin from the Start menu.
Run the Script
- Open a Cygwin terminal.
- Change to the script’s directory (Cygwin uses Windows paths with a
/cygdrive/prefix):
cd /cygdrive/c/Users/YourName/scripts
- Make the script executable:
chmod +x your_script.sh
- Execute it:
./your_script.sh
Cygwin provides a near‑complete Linux environment, making it suitable for scripts that rely on advanced Unix utilities.
Method 4: Running a Bash Script Directly from PowerShell
While PowerShell isn’t a Bash shell, you can invoke a Bash script if WSL or Git Bash is installed.
Using WSL from PowerShell
- Open PowerShell.
- Run the script via WSL:
wsl /bin/bash /path/to/your_script.sh
Using Git Bash from PowerShell
- Open PowerShell.
- Call Git Bash with the script path:
"C:\Program Files\Git\bin\bash.exe" -c "/c/Users/YourName/scripts/your_script.sh"
These approaches let you stay within PowerShell while still leveraging the Bash interpreter Worth keeping that in mind..
Common Issues and Troubleshooting
- Permission denied: Ensure the script has execute permission (
chmod +x). - Line ending errors: Scripts created on Windows often use CRLF line endings, which can confuse Bash. Convert them to LF using a text editor or the
dos2unixutility. - Shebang mismatch: Verify the shebang points to the correct interpreter (e.g.,
#!/bin/bash). - Missing commands: Some Bash commands may not exist in Git Bash or Cygwin; install the required packages or adjust the script accordingly.
- Path issues: Use absolute paths or ensure the script’s directory is in the
PATHenvironment variable.
FAQ
Q1: Can I run a shell script without installing any extra software?
A: Not directly. Windows’ native command