How to Create & Run Shell Scripts on Windows [Step-By-Step]
You will need to enable the Windows Linux Subsystem before creating a script
3 min. read
Updated on
Read our disclosure page to find out how can you help Windows Report sustain the editorial team. Read more
Key notes
- A shell script can help carry out repetitive automated tasks such as backup and software installations via the command line.
- It is possible to run a shell script file in Windows using the Windows Subsystem for Linux.
A shell script is a shell programming language program typically used on Linux and another Unix-based operating system. However, you can create and run shell scripts on Windows with additional steps.
Here in this guide, we have mentioned a step-by-step method to run a shell script on Windows. Let’s get started!
How can I create and run shell scripts on Windows?
1. Install the Windows Subsystem
- Press Windows + I to open Settings.
- Go to Privacy & Security, and click For developers.
- Click the toggle switch next to Developer mode. Now click Yes on the Use developer features window.
- Press the Windows button and type Turn Windows features. Next, select the option displayed and click Open.
- Locate Windows Subsystem Linux and place a checkmark next to it. Click OK.
- Follow the onscreen instructions and restart your PC to finish.
2. Download Ubuntu & Integrate it with WSL
- Press the Search button and type Microsoft Store.
- Launch the store app and type Ubuntu in the search bar.
- Click Ubuntu from the search and click on Get to download it.
- Search for the app and launch it.
- The Linux command prompt will install on your disk.
- The integration with WSL will also initiate.
- Enter the username and password and note it down for future use. The process might take some time to complete.
3. Create a shell script
- Go to the desired location on your computer, press the Shift key, and right-click on the blank space.
- From the context menu, click the Open PowerShell window here.
- Type the following command to get redirected to the bash shell:
bash
- Next, copy and paste the following command after replacing the filename with the name of your file and press Enter to create a shell script file:
cat>filename.sh
- Check the created file and go back to PowerShell. Press Ctrl + C to terminate the process.
- To run the shell script, you need to add some data. On the PowerShell window, type the following command after replacing the filename with the name of the file and press Enter. This command will open a file for editing:
vi filename.sh
- Press the I key to access the file and start editing it.
- Type the following command in the file:
echo “Hello World”
- To save the contents of the file and close it, press the Esc key from the keyboard and type the following command, and press Enter
: wq
4. Run the Shell Script
4.1 Use the PowerShell
- Go to the location where your sh file is present, press the Shift key, and right-click the blank space.
- From the context menu, click the Open PowerShell window here.
- Type the following command and press Enter to go to the bash shell:
bash
- Next, copy and paste the following command and press Enter:
sh filename.sh
- You will see the output.
4.2 Use Command Prompt
- Press the Windows key, type CMD and click Open.
- Type the following command and replace the E:/New folder with the path of your file, and press Enter:
cd /d E:/New folder.
- Next, copy and paste the following command, replace the filename with the name of the file, and press Enter:
bash filename.sh
- You will get output on your command prompt window.
So, these are steps that you can follow to run shell script commands on Windows. Try this step-by-step guide and if you have any questions, let us know in the comments section below.
User forum
0 messages