How to run shell script in windows

  • To run shell scripts on Windows, you must enable WSL and Virtual Machine Platform, install a WSL distro, and use “Bash Script.sh” to execute it.
  • Alternatively, use third-party tools like Cygwin, Cmder to run Linux commands on top of Windows OS.

If you have worked with Linux distributions and repositories, you would know what shell scripts are. To run shell scripts on top of a Windows computer can be challenging, as the process is not quite straightforward.

Shell scripts are a piece of code written in plain text but have the “.SH” file extension. These are usually used to automate tasks and execute Linux or Unix packages. These are written in Bash and start with “#! /bin/sh“.

You do not need to install a Linux or Unix distribution alongside your Windows operating system to run a shell script. In this article, we’ll show you how to run a shell script on a Windows PC.

On This Page

What are Shell (.SH) Files

The Linux/Unix operating system uses shell or .SH script files to perform certain tasks. In comparison, it can be substituted by Windows batch files for the Windows operating systems, which are used to execute commands on a computer.

The Batch language is a simple and interpreted programming language, unlike C++ or C#. However, you do not have to translate a shell script file to batch to perform similar tasks on a Windows computer, as you do on a Linux PC.

There are different methods to run the same .SH file on a Windows computer as on a Linux system. Let us show you how to run a shell file on a Windows PC.

Run Shell Script (.SH) File on Windows

There are both native and third-party methods to run shell files on a Windows computer. You can use Cygwin, or a third-party tool of your choice, to run a shell script on Windows OS. Alternatively, you can install the Windows Subsystem for Linux feature on Windows and then execute the shell script file through it.

Below you’ll find the methods to run the file using both methods.

Run Shell Script File using Windows Subsystem for Linux (WSL)

This section has been divided into 3 steps to make the process easier to understand. You must do the following 3 things to run a shell script file in the given order:

  1. Install WSL
  2. Install a Linux distro
  3. Run the shell script file

Note: If you are performing these steps on a Virtual Machine, then you will need to enable Nested Virtualization.

Install WSL

Use the following steps to install WSL on your computer:

  1. Press the Windows Key + R shortcut keys to open the Run Command box.

  2. Type in “optionalfeatures” and press Enter.

    Open the Optional Features applet

    Open the Optional Features applet
  3. Select “Windows Subsystem for Linux” and “Virtual Machine Platform,” and then click Ok.

    Enable WSL and Virtual Machine Platform

    Enable WSL and Virtual Machine Platform

    The wizard will now install WSL.

  4. When the installation is completed, click “Restart now.”

    Restart computer

    Restart computer

Once the computer restarts, it is time to install a Linux distro.

Install a Linux Distribution

Use the following steps to install a Linux distribution:

  1. When it restarts, press Windows Key + R again to open Run.

  2. Type in “cmd” and press CTRL + Shift + Enter to run Command Prompt as administrator.

  3. Run the following command to obtain a list of available Linux distros:

    wsl --list --online

    Get list of all available Linux distros in Command Prompt

    Get list of all available Linux distros in Command Prompt

    Note down the name of the distro you want to install.

  4. Use the following command to install the preferred distro while substituting its name:

    wsl --install -d [DistributionName]

    Install a Linux distro in WSL

    Install a Linux distro in WSL

    The distro will now begin to download and install.

  5. Set up a username and password.

    Set up username and password

    Set up username and password
  6. Restart the computer.

Now, the next step is to simply run the shell script file.

Run the Shell Script File

Follow the simple steps below to run the shell script file on a Windows PC:

  1. Press the Windows Key + R again to open Run.

  2. Type in “cmd” and press CTRL + Shift + Enter to run Command Prompt as administrator.

  3. Use the CD cmdlet to change the directory to the location of the shell script file.

    CD [PathToShellScriptFile]

    Change directory to the shell script file location

    Change directory to the shell script file location
  4. Now, use the following command to run the shell script file while substituting [ShellFileName] with the complete and correct name of the shell file:

    Bash [ShellFileName].sh

    Run the shell script file using Windows Subsystem for Linux

    Run the shell script file using Windows Subsystem for Linux

This is how you run a shell script (.SH) file on a Windows computer using WSL.

If this seems like a lengthy process for you, then you can also use the alternative method given below.

Run Shell Script File using Third-Party Tools

You can also run a shell script file on a Windows computer using a third-party tool. In the steps below, we have used Cygwin.

Follow these steps to run a .SH file on a Windows PC:

  1. First, download and install Cygwin from their official website.

  2. Once installed, run the Cygwin app.

  3. In the Cygwin terminal, use the CD command to change the directory to the shell script file location.

    Note: In Cygwin, the syntax to change the directory is different from Command Prompt or PowerShell. Use the following command syntax and example in the image to change your directory in Cygwin.

    CD /cygdrive/[driveLetter]/[Subfolder1]/[Subfolder2]

    Change directory to the shell script file location in Cygwin

    Change directory to the shell script file location in Cygwin
  4. Now, make the shell script file executable through this command:

    chmod +x [ShellFileName].sh

    Make the shell script file executable

    Make the shell script file executable
  5. Now, use the following syntax to run the shell script file:

    ./[ShellFileName].sh

    Run shell script file on Windows using Cygwin

    Run shell script file on Windows using Cygwin

These are the two methods to run shell script files (.SH) on a Windows PC. Of course, there are many other third-party tools available that allow you to run Linux and Unix files on a Windows computer.

Frequently Asked Questions (FAQs)

How to create a shell script (.SH) file?

I you know what you want to write inside the shell script file, all you need to do is follow these simple rules:
– Create a new notepad file
– Start the script with “#! /bin/sh”
– Write the code below it
– Save the file with a “.sh” extension
– To run the script, type “bash [FileName].sh” after navigating to the folder.

Can I run shell scripts on Windows?

Yes, shell script files can be executed on a Windows computer using Windows Subsystem for Linux, or third-party tools like Cygwin, kiTTY, ConEmu, Cmder, etc.

Why use shell scripts on Windows?

Shell scripts are used to perform repetitive tasks to save time and resources. They are also used to automate tasks. With the WSL feature, Microsoft has opened doors to natively run shell scripts on Windows operating systems.

How can I run .sh on Windows 7 Command Prompt? I always get this error when I try to run this line in it,

app/build/build.sh

error,

'app' is not recognized...

or,

bash app/build/build.sh

error,

'bash' is not recognized...

Any ideas what have I missed?

Here the screen grab,
enter image description here

asked Oct 23, 2014 at 6:59

Run's user avatar

1

Install GIT. During installation of GIT, add GIT Bash to windows context menu by selecting its option. After installation right click in your folder select GIT Bash Here (see attached pic) and use your sh command like for example:

sh test.sh

enter image description here

answered May 27, 2016 at 8:15

Faisal Mq's user avatar

Faisal MqFaisal Mq

5,0264 gold badges35 silver badges39 bronze badges

4

The error message indicates that you have not installed bash, or it is not in your PATH.

The top Google hit is http://win-bash.sourceforge.net/ but you also need to understand that most Bash scripts expect a Unix-like environment; so just installing Bash is probably unlikely to allow you to run a script you found on the net, unless it was specifically designed for this particular usage scenario. The usual solution to that is https://www.cygwin.com/ but there are many possible alternatives, depending on what exactly it is that you want to accomplish.

If Windows is not central to your usage scenario, installing a free OS (perhaps virtualized) might be the simplest way forward.

The second error message is due to the fact that Windows nominally accepts forward slash as a directory separator, but in this context, it is being interpreted as a switch separator. In other words, Windows parses your command line as app /build /build.sh (or, to paraphrase with Unix option conventions, app --build --build.sh). You could try app\build\build.sh but it is unlikely to work, because of the circumstances outlined above.

answered Oct 23, 2014 at 7:33

tripleee's user avatar

tripleeetripleee

176k34 gold badges275 silver badges318 bronze badges

1

The most common way to run a .sh file is using the sh command:

C:\>sh my-script-test.sh 

other good option is installing CygWin

in Windows the home is located in:

C:\cygwin64\home\[user]

for example i execute my my-script-test.sh file using the bash command as:

jorgesys@INT024P ~$ bash /home/[user]/my-script-test.sh 

answered Jun 9, 2015 at 23:09

Jorgesys's user avatar

JorgesysJorgesys

124k23 gold badges334 silver badges271 bronze badges

6

you can use also cmder

Cmder is a software package created out of pure frustration over the absence of nice console emulators on Windows. It is based on amazing software, and spiced up with the Monokai color scheme and a custom prompt layout, looking sexy from the start

Screenshot

cmder.net

answered Oct 18, 2016 at 17:05

vipmaa's user avatar

vipmaavipmaa

1,04216 silver badges25 bronze badges

1

Install the GitBash tool in the Windows OS. Set the below Path in the environment variables of System for the Git installation.

<Program Files in C:\>\Git\bin

<Program Files in C:\>\Git\usr\bin

Type ‘sh‘ in cmd window to redirect into Bourne shell and run your commands in terminal.

answered Aug 21, 2020 at 19:36

TechRookie's user avatar

TechRookieTechRookie

1712 silver badges4 bronze badges

1

On Windows 10 Anniversary Update, it’s even easier to run shell commands in/with bash on ubuntu on windows

I was trying to set my region for my x-wrt r7000 netgear router, I found the following worked for me, using bash on ubuntu on windows, you do have to enable subsystem found in windows features, and dev mode on

ssh [email protected] < /mnt/c/ccode-eu.sh

Martin Tournoij's user avatar

answered Aug 25, 2016 at 21:10

SignedAdam's user avatar

SignedAdamSignedAdam

711 silver badge1 bronze badge

1

Personally I used this batch file, but it does require CygWin installed (64-bit as shown).
Just associate the file type .SH with this batchfile (ExecSH.BAT in my case) and you can double-click on the .SH and it runs.

@echo off
setlocal

if not exist "%~dpn1.sh" echo Script "%~dpn1.sh" not found & goto :eof

set _CYGBIN=C:\cygwin64\bin
if not exist "%_CYGBIN%" echo Couldn't find Cygwin at "%_CYGBIN%" & goto :eof

:: Resolve ___.sh to /cygdrive based *nix path and store in %_CYGSCRIPT%
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%~dpn1.sh"') do set _CYGSCRIPT=%%A
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%CD%"') do set _CYGPATH=%%A

:: Throw away temporary env vars and invoke script, passing any args that were passed to us
endlocal & %_CYGBIN%\mintty.exe -e /bin/bash -l -c 'cd %_CYGPATH%;  %_CYGSCRIPT% %*'

Based on this original work.

answered Feb 21, 2017 at 12:23

Anonymouse's user avatar

AnonymouseAnonymouse

9359 silver badges20 bronze badges

just install git and by «bash <name.sh>» run your .sh file.

answered Apr 3, 2021 at 8:51

Ali Ganjbakhsh's user avatar

I use Windows 10 Bash shell aka Linux Subsystem aka Ubuntu in Windows 10 as guided here

answered Apr 9, 2020 at 6:20

Nam G VU's user avatar

Nam G VUNam G VU

33.4k69 gold badges234 silver badges374 bronze badges

Have you tried cding to the root directory where your .sh is located in order to execute it from there, instead of writing down a path to the file as you showed in your question?

Like so:

$ cd app/build
$ build.sh

answered May 28, 2021 at 6:59

D4V1D's user avatar

D4V1DD4V1D

5,8053 gold badges31 silver badges65 bronze badges

Download Windows Speedup Tool to fix errors and make PC run faster

Shell Scripts or .SH files are like batch files of Windows which can be executed in Linux or Unix. It is possible to run .sh or Shell Script files in Windows 11 or Windows 10 using Windows Subsystem for Linux. In this post, we will show you how to run a Shell Script file in Windows 11/10.

BASH is a Unix shell and command language which can run Shell Script files. You do not need to install Ubuntu or any other Linux Distros unless your scripts need the support of the real Linux kernel. We will share both methods.

  1. Execute Shell Script file using WSL
  2. Execute Shell Script using Ubuntu on Windows 11/10

1] Execute Shell Script file using WSL

Install WSL or Windows Subsystem for Linux

How to run .sh or Shell Script file in Windows

Go to Settings > Update & Security > For Developers. Check the Developer Mode radio button. And search for “Windows Features”, choose “Turn Windows features on or off”.

Scroll to find Windows Subsystem for Linux, check the box, and then install it. Once done, one has to reboot to finish installing the requested changes. Press Restart now.  BASH will be available in the Command Prompt and PowerShell.

Execute Shell Script Files

  1. Open Command Prompt and navigate to the folder where the script file is available.
  2. Type Bash script-filename.sh and hit the enter key.
  3. It will execute the script, and depending on the file, you should see an output.

Run Shell script files from Command Prompt

On a Linux platform, you usually use SH, but here you need to use BASH. That said, BASH in Windows has its limitations, so if you want to execute in a Linux environment, you need to install Ubuntu or anything similar.

2] Execute Shell Script using Ubuntu on Windows 11/10

Make sure you have Ubuntu or any other Linux distros installed.  Ubuntu will mount or make all your Windows directories available under /mnt. So the C drive is available at /mnt/C.  So if the desktop will be available at /mnt/c/users/<username>/desktop.

Run Script files in Windows via Ubuntu

Now follow these steps

  1. Type Bash in the Run prompt, and it will launch the distro prompt.
  2. Navigate to the folder using “cd” command to the folder where the scripts are available.
  3. Type “sh script.sh” and hit Enter.

It will execute the script and if they have a dependency on any of the core Linux features.

Since Linux is now available in Windows, you need not use any third-party applications like Cygwin. WSL should be enough for most of the scenarios to help you run a shell script in Windows 11/10.

How do I create a .SH file?

The steps to create a shell script file are:

  • Create a file using an editor
  • Name script file with extension . sh
  • Start the script with #! /bin/sh
  • Write the desired code
  • Save the script file as ABC.sh
  • To execute the script type bash ABC.sh.

Can we run shell script on Windows?

Yes, now with Bash Shell in Windows 11 and Windows 10, you can now create and run Bash shell scripts and incorporate Bash commands into a Windows batch file or PowerShell script.

How do I run a script in Windows?

To run a script in Windows, you can use the wscript.exe command followed by the path to the script file. Open Command Prompt, navigate to the location of your script and type wscript.exe name.vbs to execute the script. You have to make sure that you have the Windows Script Host enabled or installed on your system.

How do I run bash on Windows?

To run bash on Windows, press Win + X and select Command Prompt (Admin). Then, type “bash” and hit Enter. To access the local file system, choose Command Prompt (Admin) and enter “bash” at the prompt. Running bash on Windows allows you to use Linux command-line tools and scripts seamlessly.

How do I run a sh file in Windows PowerShell?

To run a .sh file in PowerShell, you can use the following command: bash script.sh. This command executes the shell script within PowerShell.

Ashish holds a Bachelor’s in Computer Engineering and is a veteran Windows and Xbox user. He has work experience as a Database and Microsoft.NET Developer. He has been a Microsoft MVP (2008-2010) and excels in writing tutorials to improve the day-to-day experience with your devices.

Scripts with a .sh suffix are generally Bourne shell scripts (or bash, or ksh, or zsh — all shells derived from the Bourne shell). (Note that on Unix-like systems, including MacOS, the .sh suffix isn’t necessary; the OS looks at the #!/bin/sh line at the top of the script to determine how to execute it.)

Windows doesn’t provide a Bourne-like shell.

You can install Cygwin, which provides a Unix-like environment under Windows — but it doesn’t have a particularly «native» environment. (Editorial comment: Cygwin is great for people like me who need to use Windows but would really rather be using Unix.)

There are other Unix-like subsystems that run under Windows, including MinGW and the Windows Services for UNIX package provided by Microsoft.

Or, instead of trying to write and run Unix-like shell scripts, you can write Windows batch files. These generally have a .bat or .cmd suffix. They use the same commands and syntax as the Windows interactive command prompt.

There’s also the relatively new Windows PowerShell; I’m not familiar with it myself, but I’ve heard good things about it.

Shell script files or SH files (.sh extension) can be really useful for software engineers when performing a wide variety of operations, from file processing to program execution and more. This scripting language commands file containing a series of commands is known to be used for Linux operating system by running it through a computer program called BASH. In relation to that, Bash shell scripts are written in the BASH (Bourne-Again Shell) language that is recognized by Linux programs. However, using it directly on Windows through Command Prompt or PowerShell terminal is not possible as the command language is not known to the system. Thankfully, there are a variety of ways how to run .sh or shell script files in Windows 10 and Windows 11.

Using shell script files in Linux is simpler than running them on Windows. In the latter system, you will need the use of other software in order to execute bash scripts as batch files that work for Windows systems. You can also try to convert script shell files to Windows executable BAT files, but it will require immense knowledge in commands of scripts and batch files, including translating a script file into a batch file. This option is complicated, but there are simpler ways to run shell scripts in Windows 10/11. Here are some of them.

Windows Subsystem for Linux

The first option is to use the Windows Subsystem for Linux or WSL, a compatibility layer for running Linux binary executables natively on Windows systems. It is applicable to most of the scenarios, allowing you to run a shell script in Windows 11/10.

WSL is not automatically available, so you have to enable it through the developer settings of your Windows device. You can do it by going to Settings > Update & Security > For developers. Toggle on the Developer Mode and confirm the prompt by selecting Yes. Next, look for the Windows Features (you can use the Find a Setting search bar) and choose the option “Turn Windows features on or off.” This will give you a window pop-up listing, wherein you should see the “Windows Subsystem for Linux” feature. Mark the box beside that item and click OK to install it. It will take a minute or two as the process will involve searching for relevant files for the feature and applying the changes, but it will soon ask you to restart your machine once the process is completed. After restarting, BASH should be already available in the Command Prompt and PowerShell.

After that, you need to get Ubuntu in order to access the Linux filesystem and directly run Linux commands and tools and Bash scripts or .sh files in a Linux environment on Windows. To do so, go to the Microsoft Store and download Ubuntu, which will give you the Linux distro. After that, open Ubuntu to allow it to make the installation on the hard disk and integrate with the WSL. You will then be asked to create a username and password to complete the process. You can also install a distro for Windows using the Power shell command “Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1604 -OutFile Ubuntu.appx -UseBasicParsing” or the curl command “curl.exe -L -o ubuntu-1604.appx https://aka.ms/wsl-ubuntu-1604“. Next, update and upgrade the Linux distro by downloading extra packages using the commands “sudo apt update && sudo apt upgrade.”

Once everything is settled, you can try to run shell scripts in Windows Command Prompt. Locate the script files stored as text files in your system, and right-click on an empty space inside the folder where the script files are located while pressing the Shift key. This will give you the option for “Open command prompt here” or “Open PowerShell Window here.” Select that option, type “bash” in the Command Prompt window or the PowerShell interface, and hit the Enter key to launch the distro prompt. This will bring you to the bash shell, meaning the Windows environment has changed to BASH. Confirming this is the new line that will appear in the terminal window. Finally, you can start typing the .sh file in the terminal window and just hit Enter.

Cygwin

Cygwin has a collection of GNU and Open Source tools that provide functionality similar to a Linux distribution on Windows. Using this will allow you to compile Unix or Linux applications or programs and run them on a Microsoft Windows operating system (OS) from within a Linux-like interface comparable to Linux BASH.

Cygwin is free, so you can download it anytime on Cygwin’s official website. Download and start the installation of the setup-x86_64.exe file. There are two versions of Cygwin: the 32-bit and 64-bit versions. Nonetheless, we suggest having the latter since “a full 32-bit Cygwin distro is not feasible anymore, and will in all likelihood fail in random places due to an issue with the fork system call,” as stated on its site. The 32-bit Cygwin is only for limited scenarios, including when there’s no way to run a 64-bit version of Cygwin. Once the GUI installer (which can be run to download a complete Cygwin installation) is downloaded, open it on the Downloads folder and follow the instructions on each screen to install Cygwin. Its site also has a detailed document for setting it up.

Once ready, open the Cygwin64 Terminal through the shortcut the installer will produce on the desktop. Provide the command “cd C:Users/Username/Desktop” to change the directory to where the script file is stored. After that, you can start executing shell scripts. Just type the SH file name together with the .sh extension (e.g., sh test.sh).

Git

Git is a free and open-source version control system ideal for a group of programmers working together or coordinating on a source code project. It can keep track of the changes you make to files over time, allowing you to see who made changes to a file at different times and revert files to a previous state.

Linux and macOS PCs have the pre-installed Git as their command line tool, but Git also has versions for all operating systems downloadable on its official website. In this case, you would want to have Git for Windows. Once downloaded, visit your Downloads folder and open the downloaded setup file to start the Git installation. Follow the instructions that will appear on the device screen as the installation progresses, and make sure to get the “Git bash Here” option checked when a window pop-up listing appears confirming your desired setup for the components. As for the other options for the rest of the components, just keep them checked as default. Just keep on selecting Next until you reach the Install button. Close the installation wizard and open the Git Bash by searching for it on your computer. You’ll see other Git-related apps but click for the one that says “Git Bash.”

Once launched, you need to change the directory to where your file is located by using the command “cd desktop.” Also, you can go to the folder where the SH file is stored and right-click on the folder to select the “Git Bash Here” option. When the Git command terminal or Git Bash appears, you can start putting the SH executing commands. Just type the SH file name together with the .sh extension (e.g., sh test.sh).

  • How to make screenshot on windows 11
  • How to remove password from windows 10
  • How to run python script on windows
  • How to set environment variable in windows
  • How to open windows firewall port