Understanding the Linux/Unix Command 'bash'

Bash is one of several shells that offer command-line access to Linux commands

When you run bash in Linux, you're invoking a session of the Bourne Again Shell. Bash is just one of a class of shells—environments that work from a terminal window to execute commands and perform interactive functions.

A shell is a text-only method of interacting with a computer. In the 1980s and early 1990s, before graphical user interfaces became as widespread, people routinely interacted with their computers through a shell. For example, before Windows 95, Microsoft's primary operating system was MS-DOS.

Why Linux Shells Matter

Nowadays, it's rare to interact with a Windows 10 computer through the Command Prompt or the PowerShell (two versions of a command-line-based interface still available on Windows). However, for Linux, shell-based interactivity is much more common. Although Linux desktop environments are more robust and complete than ever, the diversity of DEs means that in some cases, the quickest and easiest method of doing something is to open a terminal window, launch a shell session, then execute the command with a keyboard instead of with mouse clicks.

Linux Shell Diversity

screenfetch for two shells

Much as Linux supports many different distributions (Ubuntu, Fedora, openSUSE, etc.) and different desktop environments (Gnome, KDE, XFCE, etc.), Linux also supports a diversity of shells, each optimized in some way to focus on different feature sets. Most people use a default shell—often, the default of the distribution—but you're free to install as many different shells as you like, and to use them simultaneously in different terminal windows, too.

Common shells include:

  • Bash: A general-purpose extension of the original Sh. It's solid and multi-purpose.
  • Tcsh: A modern version of the venerable C Shell, Tcsh offers command and filename completion and it coheres, syntactically, to C-language conventions.
  • Ksh: The Korn shell, which is both a programming and a scripting environment.
  • Zsh: Zsh is extensible and scriptable, and it supports indexing on several different dimensions.
  • Fish: Developed in 2005, it aims to be fully scriptable and easy-to-use.

Bash is typically installed by default. It's a common and easy-to-use shell.

More About Bash

If Bash is your default shell, then when you launch a terminal window and see a prompt ending with a hashtag or a dollar sign, then you're in bash. To confirm, enter the following command:

basename $(readlink /proc/$$/exe)

Whatever returns as an answer is the current shell.

When you're in Bash, you're free to execute standard commands. For example, the cd command changes directories and the ls command lists the contents of a directory.

The shell also returns simple math results. For example, if you enter:

expr 2 + 2

the number 4 returns to standard output.

Most shell commands run commands the same way; they differ in how the shell itself works. Bash, for example, supports a few helpful tools:

  • Tab Completion: Bash recognizes your intent and, where possible, completes commands for you. For example, if you've only got one file in your home directory—an awkwardly named file like dat_3452643_ss_4356.txt—you can list its contents using the cat command by typing cat d then pressing the Tab key. Assuming no other files beginning with D occupy the current working directory, then pressing Tab automatically expands the full filename.
  • Command History: Pressing the up-and-down arrow keys from the shell prompt cycles forward and backward through your command history, thus saving you time re-typing commands.

How to Switch, Check or Close a Shell

Many desktop-grade Linux distributions use Bash as the default shell, but it's not the only one. For example, the popular Z Shell (usually called zsh) offers a slightly different look-and-feel.

From any terminal window, you can switch shells simply by invoking them. To change from Zsh to Bash, just type bash.

To check which shell you're in, type echo $0.

To close a shell session, type exit. Exiting the initial spawned shell for your terminal window will usually close the terminal application altogether.