The new version of WSL runs a true Linux kernel inside of a virtual machine. This means that any Linux distribution you run under WSL includes Bash.
How to Install WSL and Bash on Windows 11
To install and run Linux operating systems that include Bash on your Windows 11 system, you’ll need to install WSL first. On Windows 11, this is a straightforward process using Windows Terminal. Do not launch CMD (Command Prompt) – Windows Terminal is a different application.
- Select the Start button, and type “terminal” in the search field. In the Windows Terminal pane select Run as Administrator. Note: If Windows Terminal doesn’t launch, you may need to update it. Visit the Microsoft Store and install the latest version of the Windows Terminal.
- Type the following command: wsl –install, at the command prompt and press Enter. This single command will download and install the latest version of the Windows Subsystem for Linux. The download is a few hundred megabytes so the installation process may take some time to finish.
- When it’s finished, you should see the message: “The requested operation is successful.” Once you see this message, restart your system to complete the WSL installation. You can type shutdown /r /t 0 in Windows Terminal to initiate the reboot.
- After your system reboots, the process will continue by installing Ubuntu as the default Linux distro. You will be prompted to enter a username and password for the Linux system.
- Once you’ve finished, Ubuntu will launch into the Bash shell automatically. You can use the same commands you would normally use in Linux, including installing Linux apps into this virtual environment.
- You can install other Linux distros if you prefer. To see which ones are available to install, reopen Windows Terminal (Powershell), type wsl –list –online, and press Enter. You will see options like Opensuse, Debian, and others.
- You can install any of these distros by typing wsl –install -d «distro name» into the Windows Terminal. The same process as the previous Ubuntu installation will repeat, and you’ll need to enter a username and password to complete the installation. Note: You can also install any Linux Distro on Windows from the Microsoft Store.
How to Launch Your Linux Distro and Use Bash
There are two methods you can use to launch your Linux distro. If you installed Ubuntu, you can select the Start menu, type Ubuntu, and select the Ubuntu app to launch it. Alternatively, you can launch the Windows Terminal and just type the command Ubuntu to launch the Linux Bash shell in the Ubuntu environment. To see a list of all available Linux commands you can use in Bash, type help -d. You can get more details about individual commands by typing help followed by the command name. For example, help printf returns command details and information about each command parameter. Some of the more common Bash commands most people use when they first get started include:
Interactive Mode: Type commands into the command-line interface (Windows Terminal). Batch Mode: Run a text file that contains all of the commands you’d like Linux to execute in order. Many people create these scripts using programming syntax.
How to Run a Bash Script in Windows
To run a script in Bash, just create a text file using your favorite file editor like Notepad and save it in a location you’ll remember. One thing that’s unique about a Bash script is that the first line must be “#!” followed by the path of your Linux bash path. To see what that is, launch Ubuntu and type which bash into the command line window. This will provide the Bash path. Create a new text file and include that first line at the top. In the case of this example, that would be: #! /user/bin/bash Follow this line with each sequential command you want Linux to run. In this example:
Line 1: Uses the echo command to display text to the user on the screen. Line 2: Combines echo with the date command to return today’s date. Line 3: Combines echo with the whoami command to return your user name.
Save this text file with a .sh extension. Remember the path to this file.
Next, open the Windows Terminal and use the cd command to navigate to the path where you’ve saved the Bash script.
To run the script, type bash «name of script».
This is a very simple example, but this demonstrates how you can use such a file to craft entire Bash script programs. Even programming statements like IF statements are available to include decision-making capabilities in your script.
The site SS64 provides an extensive resource with all available Bash commands that you can use in the Bash terminal or your Bash scripts.
You can also see a tutorial on using a Linux shell script to back up a directory.