How To Create And Run Shell Script File On Windows 10

For those who think “Can a shell script file be created and executed on Windows system?” here is the answer Yes. Well to know how can it be done, go through this article completely till the end. It is a bit lengthier process but easy to follow. So we will divide the process into 4 parts.

  1. Enable WSL – One cannot directly execute the shell scripts on the Windows system. To do so, you need to first install Windows Subsystem for Linux (WSL). WSL is a feature of the Windows operating system that enables you to run a Linux file system alongside your traditional Windows apps.
  2. Download Ubuntu and Integrate with WSL – WSL comes with the Windows operating system, but you must enable it and install a Linux distribution before you can begin using it. Here we will look at how to download and install Ubuntu from Microsoft Store App.
  3. Create Shell script file – .sh files are the shell script file extensions. Let us learn how to create a shell script file on the Windows system
  4. Execute the shell script file – Run the created .sh file

Part 1: Enable Windows Subsystem for Linux (WSL)

 

Step 1: Open Settings using Windows + I keys together. Click on Update & Security option.

 

Update&security

Step 2: From the left pane click on For developers 

 

For Developers

 

Step 3: Enable the developer mode by clicking on the toggle bar.

 



Developer Mode

 

Step 4: From the pop up click on Yes.

 

Use Developer Features

 

Step 5: In the Find a setting search bar, type turn windows, and from the results click on Turn Windows features on or off

 

Turn Windows On

 

Step 6: From the opened window tick the checkbox beside Windows Subsystem for Linux to enable it. And click on OK.

 

Wsl

 

Step 7: It will take a few minutes to finish the process and it will ask for a restart. Restart the system.

Part 2: Download Ubuntu And Integrate With WSL

 

Step 1: Once restarted, open Microsoft Store App by typing Microsoft store in the Windows search bar and double clicking on it from the search results.

 

Microsoft Store

 

Step 2: In the Microsoft Store App search bar, type ubuntu and select it from the list.

 

Ubuntu

 

Step 3: Click on Get to download ubuntu, it will take a few minutes.

Step 4: From the Windows search bar type Ubuntu and from the search results double click on it to open it.

 

Open Ubuntu

 

Step 5: Linux command prompt will open and begins its installation on your hard disk and its integration with the WSL.

Step 6: It will ask for a username and password as credentials enter your desired username and password. Make sure you remember them for later use.

 

Linux Username

 

Step 7: Wait for the completion of the installation process.

Part 3: Create The Shell Script (.sh) File

 

Step 1: Navigate to the location where you want to create the .sh files on Windows. Hold the Shift key and right-click on the empty space.

Step 2: From the appeared list click on the Open Powershell window here.

 

Open Powershell

 

Step 3: In the PowerShell type bash and hit enter. This will redirect to the bash shell

 

Bash

 

Step 4: In the PowerShell type cat > filename.sh here the “filename” is the name of your file. It is your choice. For example, here I have given “test” as the filename.

Step 5: Hit enter button on the keyboard. Now a shell script file will be created in the chosen location.

 

Create File

 

Step 6: Check that the file will be created in the location and come back to the Powershell and press the ctrl + c keys together to terminate the process.

 

Test File

Step 7: To execute this shell script and see the results, you will need to add some data to the file. So let us add a simple command in the file to print Hello World.

Step 8: For this type vi filename.sh here the “filename” is the name of your file. For example, here since I have given “test” as the filename the command would be vi test.sh

 

Open File

 

Step 9: This command will open the file for editing, press the I key from the keyboard to get into the file and start editing it.

Step 10: Type echo “Hello World” in the file and to save and quit, press the Esc key from the keyboard and type :wq and hit enter.

 

Edit File

 



Step 11: This will save the contents of the file and close the file.

 

Part 4: Execute/Run The Shell Script File

 

Step 1: On your Windows system, go to the location where the sh files are present. Hold the Shift key and right-click on the empty space.

Step 2: From the appeared list click on the Open Powershell window here.

 

Open Powershell

 

Step 3: In the command prompt type bash and hit enter. This will redirect to the bash shell

 

Bash

 



Step 4: Type sh filename.sh and hit enter. For example, here the file name is ‘test’ so I will execute or run the file using the sh test.sh command.

Step 5: Now you can see the output. That’s it done!

 

Run File

 

Note: You can also execute the sh file in the Command Prompt

Step 1: Open the Run prompt using Windows + R keys together and type cmd in it. Hit enter.

 

Command Prompt

 

Step 2: This will open the command prompt. Navigate to the path where you have the sh file to execute using cd I have the sh files in a folder called New folder under Desktop so the command would be cd Desktop/New folder

 

Navigate

 

Step 3: Now type bash filename.sh and hit enter. The filename is the name of your file. For example, I will give bash test.sh

Step 4: You can see the output on your command prompt.

 

Command Prompt Output

 

 

That’s it! Hope this article is useful. Thank you!!