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.
How to run .sh or 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.
- Execute Shell Script file using WSL
- Execute Shell Script using Ubuntu on Windows 11/10
1] Execute Shell Script file using WSL
Install WSL or Windows Subsystem for Linux
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
- Open Command Prompt and navigate to the folder where the script file is available.
- Type Bash script-filename.sh and hit the enter key.
- It will execute the script, and depending on the file, you should see an output.
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.
Now follow these steps
- Type Bash in the Run prompt, and it will launch the distro prompt.
- Navigate to the folder using “cd” command to the folder where the scripts are available.
- 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.