Programming Languages

How to Install Bash

In this blog post, we’ll teach you have to install Bash on your system.

 

As a note, almost all Linux distributions have Bash preinstalled. Thus, if you work on Linux, this blog post isn’t necessary. If your distribution uses Zsh, we recommend you keep using it for its interactive features. However, to develop scripts that are as portable as possible, the parallel installation of Bash is recommended. This parallel installation can be achieved with the package management command for your distribution; for example, on Debian and Ubuntu, you would use sudo apt install bash.

 

On macOS, Bash is only available in the outdated version 3.2. In future versions of macOS, Bash is expected to be removed entirely. In this respect, Zsh represents a compatible, and in many ways even better, replacement. If you prefer Bash or use it for script programming, the best approach is to set up the Homebrew package manager first (see https://brew.sh). Then, you can install Bash in the terminal using the brew install bash command. To start Bash, run /opt/homebrew/bin/bash.

 

The installation of Bash is only difficult on Windows. In this section, we’ll present different ways you can use Bash on Windows. All of these options have their benefits and drawbacks. But let us say this first: No variant is really ideal.

 

Using Bash in a Virtual Machine

Perhaps the most straightforward way to using Bash is to install a common Linux distribution (e.g., Ubuntu) in a virtualization program (i.e., in Oracle VM VirtualBox, VMWare, or Microsoft Hyper-V). Inside the virtual machine, you’ll then have Bash available in the terminal window.

 

For learning Bash, this solution is ideal. However, you cannot access the Windows file system via your scripts. For example, if you develop a script that processes image files, you can apply the script only to files within the Linux file system. (Some virtualization systems allow you to share a directory between Windows and the Linux virtual installation. Especially with VirtualBox, however, the procedure is quite cumbersome.)

 

Another disadvantage of the virtualization solution is that you’re actually no longer working on Windows, but on Linux. You’ll have to get used to an unfamiliar desktop and get at least a little bit into Linux administration. But you do want to learn Bash though, don’t you? (From the author’s personal point of view, using Linux is an advantage, not a disadvantage.)

 

Another argument speaks against the virtualization solution: The free and therefore correspondingly popular VirtualBox program, in experience, often causes trouble on computers that are used for software development. This problem stems from the fact that various Hyper-V functions are active on such computers. Theoretically, VirtualBox should be compatible with it, but in real life, that’s more often not the case.

 

Windows Subsystem for Linux

From one point of view, the best solution for using Bash on Windows is provided by the Windows Subsystem for Linux (WSL). This function allows Linux to be run in a terminal window. What’s great about this approach is that you can access your Linux files in Windows and your Windows files just as easily in WSL. (In current WSL versions, you can even run Linux in graphics mode. But for our purposes that isn’t necessary at all.)

 

The latest version of WSL is available free of charge from the Microsoft Store. After installation, followed by a mandatory Windows restart, you must search again for Ubuntu 22.04 LTS (or from April 2024 for the then current version 24.04) in the Microsoft Store. The installation process and your first start should take about two minutes. You’ll have to enter a password twice, and you must make sure you remember this password! You’ll need it later when you want to install additional software components using sudo. (Besides Ubuntu, other distributions are also available in the Microsoft Store, including AlmaLinux, Debian, openSUSE, and Oracle Linux. Use the one you’re most familiar with.)

 

Subsequently, you can open a window via Bash at any time in the Windows Terminal or in the Start menu (program name Ubuntu n.n).

 

Ubuntu with Bash in a Terminal Window on Windows

 

Within Bash, you can access your Windows user directory via the following path (assuming your Windows installation is on the C: drive):

 

/mnt/C/Users/<name>

 

Conversely, you can find your Linux/Bash user directory in Windows Explorer via the Linux entry at the bottom of the sidebar or via the following address:

 

\\wsl.localhost\Ubuntu-22.04\home\<name>

 

Easy Access to the Linux Home Directory in Windows Explorer

 

Git Bash

The Git version management program provides a rather elegant entry point into Bash. When you install Git on Windows (see https://git-scm.com/download/win), Git Bash is installed at the same time. If you work in software development, you most likely already have Git installed anyway. (You don’t work without Git, do you?) Then, you can run Git Bash from the Start menu, which will open a terminal window running Bash version 4.4, which is not the latest version, but it is sufficient for our purposes.

 

Git Bash uses the Windows file system directly. In the Git Bash window, the most important commands are available that are also commonly used on Linux or macOS, namely, ls, cat, less, more, find, and grep, among others.

 

The problem with Git Bash is that you cannot install any other commands. Sooner or later (more likely sooner), you’ll get to the point where you want to call a command in your script that is not part of the basic Git Bash. As far as Bash programming is concerned, you’ll then hit a dead end.

 

Docker

Provided you’re familiar with the Docker container system, you can also run Bash scripts in a Docker container. However, this approach is more suitable for professionals to share scripts. To learn Bash interactively, Docker is not the ideal way.

 

Bash Configuration (/etc/profile and .bashrc)

Several files affect the behavior of Bash. Most important are /etc/profile with its global settings and .bashrc in your home directory with individual options. Note that when running scripts outside your account, only /etc/profile is considered.

 

For normal use, you do not need to make any changes to either file. The most common reason to change one of the files after all is to call commands or programs that have been installed away from the usual directories. For this task, you must extend the PATH environment variable. The following example shows how you can include /my/own/ scripts in addition to the default directories:

 

# at the end of .bashrc or /etc/profile

export PATH=/my/own/scripts:$PATH

 

Editor’s note: This post has been adapted from a section of the book Scripting: Automation with Bash, PowerShell, and Python by Michael Kofler.

Recommendation

Scripting
Scripting

Developers and admins, it’s time to simplify your workday. With this practical guide, use scripting to solve tedious IT problems with less effort and less code! Learn about popular scripting languages: Bash, PowerShell, and Python. Master important techniques such as working with Linux, cmdlets, regular expressions, JSON, SSH, Git, and more. Use scripts to automate different scenarios, from backups and image processing to virtual machine management. Discover what’s possible with only 10 lines of code!

Learn More
Rheinwerk Computing
by Rheinwerk Computing

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

Comments