Bash Shell scripts are text files ending with the .sh extension, and it contains coded information written for the Linux operating system. Script files are designed to perform a designated task when run through a computer program called BASH in the Linux platform.
However, the Windows operating system does not know the command language used inside the shell script files and therefore fails to execute shell scripts directly. If you want to know how to run a shell script in Windows, you have come to the right place. Read on to learn more about bash scripts.
What are Shell Scripts or SH files?
Shell script files (or SH files) contain one or more commands written in the BASH (Bourne-Again Shell) language and are used by multiple Linux programs. Software engineers developed these files to ease the workload and divide individual operations into numerous script files. Hence, they have a crucial role to play while creating a short but powerful computer program.
In Linux, these script files can be run in a terminal window by simply navigating to their location and typing their filename. In Windows, however, they require additional software or a Linux-like environment to execute bash scripts as batch files.
A Linux environment can be implemented in Windows through the latest inclusion of the Windows Subsystem for Linux (WSL) for Windows 10/11, through which various Linux script files or GUI can be executed through the Windows Bash Shell. Now without further ado, let us show you how to run shell scripts in Windows.
How to run .sh or Shell Script file in Windows 11/10
Batch files (or .bat files) are small coded programs for Windows that perform automated tasks with minimal effort. Similarly, a .sh script file performs the same job for Unix or Linux systems through the bash shell. Bash scripts consist of command line inputs (such as cd, md, mkdir) bundled together to perform at once upon execution. Moreover, it is possible to run Linux scripts in Windows 10/11 in the following ways:
1] Execute Shell Script file using Windows Subsystem for Linux
It is not possible to run script files through the command prompt or PowerShell terminal provided by Windows directly, but it is possible to run native Bash shell through the latest integration of WSL or Windows Subsystem for Linux being provided from Windows 10 Insider Preview Build 14316. The process involves a lot of steps as a whole, so we have divided it into 4 parts.
Part 1: Enable WSL and developer settings from Windows features:
- First, we need to enable the WSL Windows feature through the developer settings. It comes bundled with the Windows operating system and doesn’t require extra data from the internet. Enable developer settings through the steps below.
- Open Start Menu and click on the Gear icon to open the Settings app.
- Head over to the Update & Security section from the Settings menu.
- Click on the “For Developers” tab in the left pane and turn on the Developer mode using the button as shown.
- Select OK on the warning message.
- Inside the “Find a setting” search bar, type Windows features and click the option to “Turn Windows features on or off.”
- You will see a window pop-up listing all the different on-demand future or deprecated Windows features that users can enable anytime. Find Windows Subsystem for Linux among them, and mark the checkbox beside it. Click on OK to confirm the changes and proceed.
- Windows will spend some time Searching for the required files to enable the Windows subsystem.
- After acquiring the Windows files, the installation manager will apply changes, so wait for it patiently.
- On successfully completing the entire process of adding the Windows Bash, You will be asked to Restart your computer. You can either Restart now or later on your own, depending on if you have some scheduled tasks left.
Part 2: Download Ubuntu from the Microsoft Store and integrate with WSL:
- After restarting, open the Microsoft Store from the Start Menu.
- Search for Ubuntu in the App Store.
- Click on Get to download the Linux distro and wait patiently.
- Upon the completion of installation, open Ubuntu from the Start menu (From the recently added app section)
- A command prompt window opens up, and Ubuntu begins its original installation on your hard disk and its integration with the WSL to get access to Windows files.
- The Windows Bash window asks you to create an account (separate from your original Windows account). Type your desired username and password as login credentials. Remember them properly as they will be required later.
- After completion, you can run Bash scripts, Linux commands (sed, awk, mkdir), Linux tools (nano, vi, python) directly on Windows. You also access the Linux filesystem on Windows.
Part 3: Initialize the Newly installed Linux Distro:
- Most Linux distro downloaded from the app store arrive with a minimal package catalog. You need to update and upgrade these packages through these commands:sudo apt update && sudo apt upgrade. Internet connectivity is required for this process to download extra packages.
Part 4: How to run shell script in Windows Command Prompt
- Head over to the location in your Windows OS where the script files are stored as text files.
- Hold down the Shift key and right-click on an empty space inside the folder. Click on the Open command prompt here. In newer Windows installation, click on Open PowerShell Window here.
- Type bash inside the command prompt window or the power shell interface. Windows redirects you to the bash shell, and a new line opens up in the terminal window. This indicates that the environment has changed from Windows to BASH.
- Type sh yourfilename.sh inside the terminal window, where “yourfilename” is the name of the sh file, followed by file extensions.
- Through the same process, you will get to know how to run script file in Windows PowerShell.
2] Using Cygwin to run Shell Script files
The Official Cygwin application is a free and open-source command-line interface for Windows. With it, Linux and Unix programs can be compiled and run on Windows using a Linux-like interface similar to the original Linux BASH. Let us see how to run shell script in Linux from Windows through Cygwin:
- Head over to the official Cygwin website. Look for executable Windows files (setup-x86_64.exe) and download either the 32-bit or 64-bit version of Cygwin.
- Open the downloaded installation file from the Downloads folder, and follow the procedure as instructed by the on-screen steps. There are different options in the installation process, so make your choice accordingly.
- If you have installed the 64-bit version of Cygwin, open the Cygwin64 Terminal by double-clicking its shortcut on your desktop.
- The terminal will have access to any files on your pc, so provide the pathname (the folder where your script file is stored) in the following way: cd C:Users/Username/Desktop
- After you change the directory, it is time to execute shell scripts through the sh command: sh yourfilename.sh, where “yourfilename” is the name of the sh file, followed by file extensions.
3] Execute Shell Script using the Git command line for Windows
Git is a distributed version control system tool that is free and open-source, and it is available for use on all operating systems. The usefulness of Git is given in detail below:
- Changes in any set of files can be tracked using Git, which is most commonly used to coordinate the efforts of multiple programmers who are working together on the source code during the development process.
- Rapidity, data integrity, and support for non-linear distributed workflows are just some of its primary objectives (thousands of parallel branches running on different systems).
Linux and macOS computers come pre-installed with Git as a command-line tool, which is the default. However, you can also get it from the official website, which has versions for all operating systems. So let us see how to run shell script in Windows git bash:
- Head over to the Official Git version control page by opening your browser and copying the given link: Download from here
- Click on the Windows link to download the git command line for your Windows platform.
- Go to the Downloads folder and open the setup file to start installing git. Go through the installation process by following the on-screen instructions, and make your choices accordingly.
- During the installation procedure, Git will install a context menu entry through which it becomes accessible to run shell script files from anywhere on the computer. Make sure to check that option in the installer.
- To run a script file, simply head over to the folder where the .sh file resides. Right-click on the folder name and select “GIT Bash Here.”
- The GIT command terminal known as the git bash appears, and the working location is already set to your script file folder.
- Run the script file using the command: sh filename.sh (The filename can be replaced with the original sh file name, followed by the file extensions)
4] Convert Script Shell files to Windows executable BAT files
This approach is not advised for typical users, but rather for those who are well-versed in the commands of scripts and batch files (bat files). For starters, we can simply update the format and extension of the SH file.
It does need the knowledge of how to translate a script file into a batch file, therefore you should avoid using this approach unless you are familiar with the formats of both of these files.
For the sample code modifications, we will demonstrate the straightforward procedures that must be followed.
- Batch files are simple scripted programs for Windows, and they are similar to Linux shell scripts. In Windows, a batch file can be executed by simply double-clicking on it.
- Firstly, we need to open a Linux sh script file using a text editor such as Notepad.
- Double-click on the sh file. When Windows says it can’t find a file to open it, click on More Options, and click on Notepad.
- The first line of the script file must be something like the one given below: #!/bin/bash
- Remove this shebang line from the code.
- If you are an expert script file coder, analyze the Linux codes used there and think of an alternative Windows version of the code to replace it with.
- Save the script file using Ctrl + S.
- Left-click on the Bash shell file, and press F2 to rename the file extensions. Modify the extension from .sh to .bat, press Enter and click Yes to confirm the changes.
- Open the Windows Run Window using the Win + R shortcut. Inside the search bar, type cmd to open the command prompt terminal.
- Change the directory to the folder path where the SH file is stored using cd folder name.
- Execute the translated batch file using the command filename.bat
- Do note that the commands that worked for Linux (such as comment notifier #) will not work for Windows, and might cause a syntax error when run. Therefore proper translation skill is important.