How to Install and Run Bash on Windows 11 - Windows Basics

Latest

Sunday, September 4, 2022

How to Install and Run Bash on Windows 11

With the rise of the open source culture, it is certainly impossible to ignore the great capabilities of the Linux operating system and replicate them to other operating systems. Microsoft understands this and it has successfully introduced Windows Subsystem for Linux (WSL).

Introduced in 2016, WSL is a Windows feature that allows you to run the Linux file system on your operating system. After installing WSL, you can run any Linux tools on your PC easily. So, if you want to run Bash on your Windows 11, here's how to do it in detail.

How to Install and Run Bash on Windows 11

Bash is the default shell for most Linux systems. First introduced in 1989 as a replacement for the Bourne shell, it also acts as a command-line interpreter for GNU-based systems.

Furthermore, you can even use it as a word processor, graphics editing application, or as your desktop, as Opensource.com explains.

So if you want to run Bash on Windows 11, you have to install WSL on your system first. Don't be intimidated by these complicated-sounding jargon; The whole installation process is quite simple. Here's how you can get started:

1. Go to the Start menu search bar, type terminal and launch Windows Terminal with admin rights.

2. Enter the following command into Terminal:

wsl --install

3. Restart your PC once the installation is complete.

As soon as your PC boots up, the installation will begin again. Your PC will start downloading and installing the Ubuntu software from here. You will soon be asked to set up a username and password. Do that and your PC will boot up quickly when you enter your password. The installation will automatically start from where you left off before starting your PC.

Finally, Ubuntu will be successfully installed on your PC. Once you're done, Ubuntu will automatically launch into the Bash shell. You can use the same commands you normally use in Linux, including installing Linux applications into this virtual environment.

And that's how you can install and use Bash on your Windows PC. In fact, with Ubuntu on a current system, a host of other nifty features like alias, command line editing, task control, and more, will also be available to you.

How to Run Bash Scripts in Windows

To run a script in Bash, simply create a text file with your favorite file editor like Notepad and save it in a location you can easily remember.

One unique thing about Bash scripts is that the first line must be “#!” followed by the Linux bash path. To see what that is, launch Ubuntu and type bash into a command line window. This will give the Bash path.

Create a new text file and include that first line at the top. In the case of this example, it would be:

#! /user/bin/bash

Follow this line with each sequential command that you want Linux to run. In this example:

  • Line 1: Use the echo command to display text to the user on the screen.
  • Line 2: Combine echo with date command to return today's date.
  • Line 3: Combine echo with the whoami command to return your username.

Save this text file with the .sh extension. Remember the path to this file.

Next, open Windows Terminal and use the cd command to navigate to the path where you saved the Bash script.

To run the script, type:

bash <<name_script>>

This is a very simple example, but this shows how you can use such a file to generate entire Bash scripts. Even programming statements like IF statements are available to include decision-making in your scripts.

The SS64 website provides a rich resource with all the available Bash commands that you can use in your Bash terminal or Bash scripts.

No comments:

Post a Comment