Bash comes installed on almost all Linux, Unix, BSD, and MacOS platforms and is often the default shell application. Some of these systems (i.e. Kali Linux) default to ZSH etc. Although shell environments are used, all the packages you need to use bash are available in the system, and you can drop into bash shell by simply typing bash and pressing enter. We should also note that bash scripts will often run smoothly under shells like “zsh”, and etc.
In Windows environments, bash is not included by default, but there are multiple ways to use bash on Windows operating systems. Let's take a look at them now:
Microsoft offers the Windows Subsystem for Linux feature on Windows 10 and later that lets users run Linux operating systems directly. This feature is one of the ways we can use Bash on Windows systems. In order to use this feature, there are a series of installation and settings that we need to configure.
First, we need to open a PowerShell console with Administrator rights and run the following command and wait for the installation to complete:
wsl --install
After the installation is complete, you can access your Linux environment by locating the Ubuntu application from the Start menu and running it. However, in some cases, you are likely to receive an error message like the one below.
If you are getting this error message, you can try two things. First to find and activate the "Virtualization Support" option in your computer's BIOS settings. If your problem is not solved even after doing this, you can restart your computer after running the following commands:
DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-VPS
bcdedit /set hypervisorlaunchtype auto
If everything went well and your Ubuntu System started working fine on WSL, it will ask us to define a username and password before it starts the bash console.
We need to make a file system related reminder. Ubuntu WSL runs on its own virtual file system. It mounts the file systems of your Windows under the /mnt/ directory. So, if you want to view the files and directories on the C:/ disk of your Windows system, you need to run the ls /mnt/c command. This is an important case you should consider while writing and running your scripts.
On Windows, to access your home directory in Ubuntu WSL environment, you should use “explorer.exe .” (note the dot at the end) in your home directory and press enter.
Cygwin is a software package designed to provide a POSIX compliant environment on Windows operating systems. POSIX is a set of standards that define how an operating system should work, and most Unix and Unix-like systems (Linux, BSD, etc.) follow these standards.
Cygwin includes a library (cygwin1.dll) that translates POSIX APIs to Windows APIs, and many GNU and open-source applications which make these applications can run natively on Windows.
Many Unix/Linux commands and services (for example, bash shell, ssh, tar, awk, make, grep, and more) become available on Windows with Cygwin. You can also compile and run Unix/Linux programs on Windows with Cygwin.
To explain how Cygwin works in details; Cygwin is a tool that allows Unix/Linux applications that know how to perform Unix/Linux operating system calls (for example, opening a file or creating a process), and these applications convert these to a format that Windows understands.
This makes the application feel "as if" it is running in a Unix/Linux environment. However, this type of conversion may not always be perfect and can sometimes cause applications to behave in unexpected ways.
To install Cygwin, you need to download and run setup-x86.exe (for 32-bit systems) or setup-x86_64.exe (for 64-bit systems) from the Cygwin website (https://www.cygwin.com). During the installation, you can choose which packages to install. For the bash shell and some basic tools, the default options are usually sufficient. After the installation is complete, you can start Cygwin and use the bash shell.
Now let's take a look at the installation steps:
After the welcome screen, we’ll see the options on how to download the files and how to install them. We can proceed with the default option.
In the next step, we need to select where to install CygWIN. We recommend leaving it as default.
In the next step, we need to select the “Local Package Directory” settings. This’ll be the directory where the packages will be downloaded. It is important for your security that you select a directory where only your account can access will provide you with security. If you don't need anything different, you can also leave this as default:
In the next step, we need to select where we want to download the packages from. You can pretty much select any download sites in the options but in cases where your connection is slow, you may need choose a mirror within your own country. Or if you have restricted access to some countries, you can choose a mirror from a non-restricted country, usually just click on any of them:
In the next step, you need to select the applications and packages you want to use with CygWIN. Here, we have selected the current versions of Bash related packages under Shells. But keep it in mind that we will need many more packages for a comfortable workspace.
And now, the installation can begin:
When the installation process is complete, you can access your shell by clicking one of the Cygwin64 Terminal icons placed on your desktop and start menu.
As you can see, it is possible to use Bash on Windows systems as well. It takes some time to get used to it to be able to use it effectively, but considering its capabilities, it's well worth it.
Also we have a hands-on "Introduction to Bash Scripting" course if you need it: