How to Run Shell Script in Windows 10/11? Tested Solutions

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 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?

How to Run Shell Script in Windows

Shell script files (or SH files) contain one or more commands written in the BASH (Bourne-Again Shell) language and 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.
Update & Security in Settings app
  • Head over to the Update & Security section from the Settings menu.
Enable Developer settings
  • Click on the “For Developers” tab in the left pane and turn on the Developer mode using the button as shown.
Enable Developer Settings
  • Select OK on the warning message.
Search for Windows Features
  • Inside the “Find a setting” search bar, type Windows features and click the option to “Turn Windows features on or off.”
Enable Windows Subsystem for Linux
  • 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.
Install Windows Subsystem for Linux
  • Windows will spend some time Searching for the required files to enable the Windows subsystem.
Applying changes
  • After acquiring the Windows files, the installation manager will apply changes, so wait for it patiently.
Restart Now to finish installation of WSL
  • On successfully completing the entire process for 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.
See also  7 Best Fixes For HP Printer Driver Is Unavailable

Part 2: Download Ubuntu from the Microsoft Store and integrate with WSL:

  • After restarting, open the Microsoft Store from the Start Menu.
Ubunty search result in the Microsoft Store
  • Search for Ubuntu in the App Store.
Download Ubuntu from the Microsoft Store
  • Click on Get to download the Linux distro and wait patiently.
Open Ubuntu in Windows Start
  • Upon the completion of installation, open Ubuntu from the Start menu (From the recently added app section)
Initialize Ubuntu with WSL
  • 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.
Finish installation by entering username and password
  • 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:

Upgrade the Linux Distro in Windows
  • Most Linux distro downloaded from the app store arrives 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.
Open shell script in Windows command prompt
  • 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.
Open script shell files in Windows Powershell
  • 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:

Official Cygwin website
  • 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.
Run Cygwin Terminal
  • 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.
See also  2 Fixes: You Must Be An Administrator Running A Console Session In Order To Use The SFC Utility

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 are 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:

Git Windows official webpage
  • 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.
Enable Git Context menu options
  • 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.
Open script files using Git Bash Here
  • 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 Script files in Git Command Line Windows
  • 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.

See also  7 Best Fixes For HP Printer Driver Is Unavailable

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.
Remove the first line of bash files
  • 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.
Rename sh file to bat file
  • 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.
Confirm renaming changes
  • Open the Windows Run Window using the Win + R shortcut. Inside the search bar, type cmd to open the command prompt terminal.
Run Command Prompt from Run
  • Change the directory to the folder path where the SH file is stored using cd folder name.
Run batch files from Windows Command Prompt
  • 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.

FAQs

How Do I Run A .Sh File In Linux?

The following is the technique to be followed in order to launch the.sh file shell script on Linux:
– Launch the Terminal program.
– Create a new .sh script file.
– Create a nano script-name-here.sh command file
– The chmod command may be used to provide execute permission to your script: chmod +x script-name-here.sh
– Type ./script-name-here.sh into your terminal.

How Do I Run A Shell Script From Another Shell Script?

There are a handful of various approaches you may take to do this:
– Make the other script executable by including the #!/bin/bash line at the beginning and adding the path to the file’s location to the $PATH environment variable. Then you may use it as you would any other command.
– Alternatively, use the source command (alias is .) to invoke it, as follows: source /path/to/script
– Lastly, you may use the bash command to run it, such as: /bin/bash /path/to/script

How Do I Search For A File In Find?

To locate a particular file by name or extension, use the find command from the command line. Example: we search for *.err files in the /home/username/ directory and all subdirectories, including the following command: find /home/username/ -name “*.err”
find expressions assume the following syntax:
find options starting/path expression

How Do I Run A PowerShell Script From The Command Line?

The PowerShell script may be executed from the command line with these steps
– Enter the complete path of the PowerShell script, for example, “C:\Example\example script.ps1,” and press enter.
– Alternatively, you may browse to the script file in the PowerShell ISE.
– Use the “Open” option on the “File” menu to open it, and then use the “File” and “Run” buttons to execute the script.