How to open shell in windows

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?

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

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

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:

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.

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.

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.

Files with the .sh extension, i.e., shell scripts, are designed to be run by Unix shells such as Bourne or GNU Bash. If you attempt to run shell scripts in Windows, it won’t work as the commands won’t be recognized by default.

Instead, you’ll have to introduce a compatibility layer to be able to run such scripts in Windows. There are various ways to do this, with the popular ones being Windows Subsystem for Linux, Cygwin, and Git Bash.

In this article, we’ve listed all the necessary steps to run Shell Scripts in Windows using these methods.

Windows Subsystem For Linux

The first method is to install a Linux distro through the Windows Subsystem for Linux feature. Cygwin and Git are convenient, but users often have difficulty running complex shell scripts and Linux applications with them. As such, WSL is the recommended option. Here are the necessary steps for this:

  1. Press Win + R, type optionalfeatures, and press Enter. 
  2. Enable the Windows Subsystem for Linux and Virtual Machine Platform features, and press OK.
    windows subsystem for linux virtual machine platform
  3. After the changes are applied, restart the computer.
  4. When booting, press the BIOS key (Fn keys or Del).
  5. Go to the Advanced CPU Configuration or similar section and look for the virtualization option. It will be named something like AMD-V, SVM, VT-d, or VT-x.
  6. Enable the virtualization option if it isn’t already so, and press F10 to save the changes and exit.
    gigabyte-svm
  7. After booting, download and install the WSL2 Linux Kernel Update Package.
  8. Press Win + R, type powershell, and press Enter.
  9. Execute the following command to set WSL 2 as the default version when installing new Linux distros:
    wsl --set-default-version 2
  10. Open the WSL Store, select your preferred distro, and press Get to download it. If you’re unable to install WSL Linux Distros from the Microsoft Store, you can directly download and install them using these links provided by Microsoft.
    get-debian-microsoft-store
  11. After it’s downloaded, press Open.
  12. On the first launch, the installation will take a couple of minutes.
  13. After the installation completes, enter a new UNIX username and password.
    wsl-debian-installation
  14. Before anything else, it’s good practice to update and upgrade your packages. On Debian-based distros, you can do this with the following command:
    sudo apt update && sudo apt upgrade
    wsl-debian-sudo-apt-update
  15. Navigate to the location of the script file.
  16. Hold Shift, right-click, and select Open Linux Shell here.
  17. Enter sh scriptname.sh to run the shell script.
    wsl-shell-script-in-windows

Note: For common WSL issues encountered both during and after the installation, we recommend referring to this troubleshooting guide from Microsoft.

Use Cygwin

Cygwin is a run time environment used to run code designed for Unix environments on Windows. Here’s how you can run shell scripts in Windows using Cygwin:

  1. Download and launch the Cygwin installer.
  2. Select Install from Internet as the download source.
  3. Specify the root directory to install Cygwin to. Also, select whether Cygwin should be available for all users or just the current one.
  4. Select the default options for local package directory and internet connection.
  5. For the download site, choose a mirror geographically close to you if possible. Otherwise, any mirror will do fine. The difference in download speed shouldn’t be anything major.
  6. Don’t select any packages for the time being. Just press Next and follow the on-screen instructions to complete the installation.
    cygwin-installation-confirm-changes
  7. Open the Cygwin64 Terminal and navigate to the location where the shell scripts are stored.
    cd <scriptpath>
  8. Use the following command to run the shell script:
    sh scriptname.sh
    cygwin-shell-script-in-windows

Use Git Bash

During its installation, Git optionally adds some UNIX tools to the PATH in Windows, with sh.exe being one of them. Here’s how you can install Git and run shell scripts using Git bash:

  1. Download and install Git.
  2. During the installation, make sure to include Git bash and add Git to the PATH variable.
    git-bash-windows-integration
  3. After the installation is complete, navigate to the folder with the shell script.
  4. Right-click on an empty area and select Show More Options > Git bash here.
  5. Once again, use the sh scriptname.sh command to run the shell script.
    git-bash-shell-script-in-windows

Anup Thapa primarily covers Windows systems, networking, and computer hardware at TechNewsToday. Anup has been writing professionally for almost 5 years, and tinkering with PCs for much longer.
His love for all things tech started when he got his first PC over 15 years ago. It was a Pentium IV system running Windows XP on a single 256 MB stick. He spent his formative years glued to this PC, troubleshooting any hardware or software problems he encountered by himself.
Professionally, Anup has had brief forays into a variety of fields from coding and hardware installation to writing. In doing so, he’s worked with people of different backgrounds and skill levels, from average joes to industry leaders and experts.
This has given him not just a versatile skill set, but also a unique perspective for writing that enables him to concisely communicate complex information and solve his reader’s problems efficiently.
You can reach out to him at anup@technewstoday.com.

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).

Are you interested in working with shell scripts on Windows? Thanks to a recent addition by Microsoft, you can now use Windows Subsystem for Linux to make this happen. 

Once you enable shell scripts in Windows 10, you can start creating shell scripts of your own. Shell scripts are great for automating simple tasks. You can also work on open-source Linux-based projects if that’s an area of interest to you. Finally, you’ll learn how to execute shell scripts on Windows 10. Continue reading to learn more.

What is Linux?

Linux is an open-source operating system that is highly popular among computer enthusiasts. Linux competes with other computer operating systems like Microsoft’s Windows, Apple’s macOS, and mobile operating systems like Android and iOS. 

The Linux operating system was originally developed by Linus Torvalds back in 1991. The Linux kernel was designed as a Unix-like operating system. Unix was an early operating system created by Bell Labs in 1969. Today, modern Linux distributions are still Unix-like, meaning they retain the basic structure and properties that Unix had. An example of a non-Unix operating system would be Microsoft Windows.

The top Linux distributions have changed over the years, but as of 2022, Ubuntu, Debian, CentOS, Fedora, and Red Hat rank as the top 5 most popular options. 

What is Bash?

When Linus Torvalds created Linux, he included a Unix shell called Bash. Bash had been created just two years before, in 1989 by Brian Fox. Bash has been the longtime default for Linux and was also the default for Apple macOS until it was replaced by Z shell in 2019. 

Until 2016, Windows users could not use the Linux kernel or Bash at all. Windows first introduced the Windows Subsystem for Linux (WSL) beta with Windows 10 version 1607 update. About a year later, in October 2017, WSL was fully released in Windows 10 version 1709. Microsoft developed WSL for hobbyists and developers who want to work on open-source Linux-based projects. 

It’s important to note that WSL is not preinstalled on Windows 10. If you would like access to create and run shell scripts on Windows 10, you will need to manually install WSL or join the Windows insider program. 

What is a shell script?

A Shell script is a type of script that cannot be run without a Unix shell. Further, a shell script is a series of commands that are executed line by line by the command line. 

You can use shell scripts to automate processes and avoid repetitive tasks. Instead of manually completing each step in a series, you can execute a script, and the command line will handle the rest.

For example, if you find yourself regularly stopping processes that are hogging your CPU, you can automate this process with a script. When you execute the script, it may be designed to find a set of processes using CPU resources and request to kill them. 

Enabling shell scripts in Windows 10

  1. Click on the Start (Windows) button and enter “Control Panel” into the search bar. Click Open on the Control Panel result on the right-hand side.
Search for Control Panel
  1. Within the Control Panel window, find and click on Programs.
Control Panel options
  1. Now, from the Programs window, find Click Turn Windows features on or off underneath the Programs and Features header.
Turn Windows features on or off
  1. In the Windows Features window, scroll to the very bottom of the window. Check the Windows Subsystem for Linux option. Then click OK.
Enable Windows Subsystem for Linux to be able to run shell scripts
  1. Windows will automatically install the necessary files. When the installation is complete, select Restart Now.
Restart Windows
  1. When your computer restarts, you need to install Ubuntu from the Microsoft store.
Install Ubuntu to run shell scripts on Windows
  1. After installation, make sure you open Ubuntu and see it up. You are now ready to use scripts on your Windows 10 machine.

If you encounter any issues with Ubuntu or bash commands not working correctly, you may want to check that Virtualization is turned on in your BIOS. The most updated WSL version, WSL 2, runs the Linux kernel using virtualization technology. This means a virtual machine needs to be able to run on your system.

Now that Windows Subsystem for Linux and Ubuntu has been installed, you are ready to start creating shell scripts in Windows 10. You may be tempted to write bash scripts with Notepad, but this is not recommended. Because Notepad is designed for Windows/DOS systems, the line endings will differ from those that are found at the end of Unix/Linux line endings. 

Text editors for shell scripts

You should use software that is designed to convert to Unix/OSX end-of-line characters. The best open-source software available for this is Notepad++. Amazingly, Notepad++ is lovingly maintained and developed by a single individual, Don Ho. 

If you try Notepad++ and don’t like it, you can try another fan favorite, nano. Nano is a text editor for Unix/Linux systems. You can easily create shell scripts that will run in bash, using nano. Download nano to get started.

Example shell scripts

Let’s look at some basic shell scripts, so you can learn more about what you are going to be coding and see how some formatting and syntax work.

1. Hello World!

echo "Hello World!"

This script will print out the infamous Hello World! Notice that echo can be used as a print command when not combined with any other modifiers. It will print the string on a new line. If you add the -n modifier, the output will print on the same line. 

2. Sum two numbers

If you want to do some basic arithmetic, you might have a script that looks like:

# Add two numbers together
((sum=25+35))

# Print the sum of the numbers
echo $sum

Note that the # symbol is used to make comments that are not expressed. The output of this script will print the sum of 25+35, which is 60. 

3. Take user input

The following script will ask for the user’s name and then use the read command to take the user’s input. Then the user’s name is passed into the following expression, ultimately welcoming you to Windows Subsystem for Linux. 

echo "What is your name?"
read name
echo "Welcome $name to Windows Subsystem for Linux."

Write basic shell scripts in Windows 10

Continue reading to learn how to write basic shell scripts in Windows 10 using Notepad++.

  1. Click the Start button and search for “Notepad++” and click Run as administrator on the right-hand side.
Search Notepad++
  1. Now you can create your script.
Write your bash script in Notepad++ on Windows
  1. Once your script is complete, you need to use the EOL Conversion option available in Notepad++. Click Edit and locate EOL Conversion from the dropdown menu. Hover over this option and then select UNIX/OSX Format from the next dropdown menu.
Change EOL Conversion to UNIX/OSX format
  1. Now select File and then Save As. Make sure to name your file something you will recognize and add .sh to make it a shell script file.

Save the sh file before you run it on Windows

  1. Once the shell script is saved, continue to the next section to learn how to run your own shell scripts.

How to run shell scripts (.sh files) on Windows 10

You’ve created your first shell scripts, and it’s time to execute the sh file. Remember that when using WSL, you can only use Linux commands and utilities. Windows 10 programs will not work in bash scripts. To execute a script file, follow these step-by-step instructions:

  1. Click on the Start (Windows) button and enter “Command Prompt into the search bar. Click Run as administrator on the Command Prompt result on the right-hand side.
  2. Navigate to the folder where the script file is saved. You move around in the command prompt using the cd command. For example, if you want to access the Documents folder, you would enter the following and press Enter:

    cd C:\Users\Username\OneDrive\Documents

    Note: Username would be the username that you set up for yourself when you registered your computer. 

Navigate to the location of the sh file in Command Prompt
  1. Now enter bash file-name.sh, where file-name is the whatever you’ve named your script file. 

    bash file-name.sh

    The script will execute, and if there are any outputs or print statements included in the script, the output will be returned.

Bash scripts running on Windows 10

You’ve made it far and learned a ton of information in one go. Command-line utilities, different operating systems, and learning to write and execute shell scripts can be difficult topics. In fact, these topics will take time to master. You have a ton of learning to do for scripting, but resources are available to help you all over the internet. 

Within this guide, you learned the basics of Linux and Bash. You learned what shell scripts are and that you need to specifically enable Windows Subsystem for Linux (WSL) to use them. You learned how to create shell scripts using Notepad++ and how to execute the scripts in bash. Enjoy experimenting!

  • How to run makefile on windows
  • How to kill process on port windows
  • How to run jar file windows
  • How to open ftp on windows
  • How to install yarn on windows