Software & OS

What Is Bash? The Bourne Again Shell Explained

Bash stands for "Bourne Again Shell". It is a command language interpreter: a program that reads commands from your keyboard, a string or a script file, and runs them.

The full name in the Bash manual page is "GNU Bourne-Again SHell". It is a reimplementation and extension of the Bourne shell, sh, and it is the shell many Linux systems use for interactive commands and scripts.

What does "Bourne Again Shell" mean?

A shell is the program between you and the operating system. You type a line such as ls -l, and the shell works out which program to start, passes it the arguments and waits for it to finish. The original Unix shell of this kind was the Bourne shell, sh. Bash is a newer implementation of the same idea, written for the GNU project, that stays compatible with sh and adds features on top. The "Bourne Again" in the name refers to that lineage.

Bash is not the terminal window. The terminal only displays text and sends your keystrokes. Bash is the process inside it.

What does Bash actually do?

The POSIX Shell Command Language specification describes what any sh-style shell does with input. Bash follows the same sequence:

  1. It reads input from a file, from the -c option, or from a terminal.
  2. It breaks the input into tokens: words and operators.
  3. It parses the tokens into simple commands and compound commands.
  4. It performs expansions, such as turning *.txt into a list of matching file names.
  5. It sets up redirections, such as > out.txt.
  6. It runs a function, a built-in, an executable file or a script, passing the arguments along.
  7. It optionally waits for the command to finish and collects its exit status.

That is why one line can do several things. In grep error app.log | sort > errors.txt, the shell starts two programs, connects the output of the first to the input of the second, and writes the result to a file.

Three ways Bash runs commands

The Bash manual page says Bash executes commands read "from the standard input, from a string, or from a file". Those are three different uses:

UseExampleWhat happens
InteractivebashYou type commands at a prompt and see each result.
A stringbash -c 'echo hello'Bash reads the first non-option argument as the command, runs it and exits.
A script filebash backup.shBash reads the commands from the file, one after another.

Example output of the second row:

$ bash -c 'echo hello'
hello

For writing your own scripts, see Bash script basics. Loops and conditionals have their own guides: Bash for loop examples.

How to check whether you are running Bash

Run these in your terminal:

bash --version
echo "$BASH_VERSION"
echo "$SHELL"

bash --version prints the version if Bash is installed. $BASH_VERSION is set only inside a running Bash, so an empty result means your current shell is something else. $SHELL holds your login shell, which can differ from the shell you are in right now, for instance after you typed zsh.

Example output on a Linux machine (your version will differ):

$ echo "$BASH_VERSION"
5.2.21(1)-release

Is Bash the same as sh?

No, though they overlap. Bash aims to conform to the POSIX Shell and Utilities standard, and in POSIX mode it "changes the shell's behavior where its default operation differs from the standard". You can turn that mode on with the --posix option. If Bash is started under the name sh, it tries to mimic the startup behaviour of the historical sh as closely as possible, and switches to POSIX mode after reading its startup files.

Practically, a script that uses only POSIX features runs under both. A script that uses Bash-only features does not. The usual rule is to start a script with #!/bin/bash if it relies on Bash features, and to avoid those features if the first line says #!/bin/sh.

Login shells and startup files

The man page defines a login shell as one whose argument zero begins with a hyphen, or one started with the --login option. A login shell reads the system and personal profile files at startup. That is why a setting you add in one file can appear in a terminal window but not in a script started by a program: the two shells start differently and read different files. The exact file names and the order they are read in are listed in the INVOCATION section of the man page.

Where you will find Bash

  • Linux: very commonly installed and often the default login shell, though the default depends on the distribution.
  • macOS: a Bash binary is present, but the default login shell is zsh. See Zsh vs Bash.
  • Windows: not the default. You can get it through Git Bash or in a Linux environment such as WSL.

Versions vary between systems, so check with bash --version before using a recent feature.

If a command says "bash: command not found"

Bash looked for the program you named and could not find it in a built-in, a function, or any directory listed in PATH. Check the spelling first, then confirm the program is installed. echo "$PATH" shows the directories Bash searches, separated by colons.

Frequently asked questions

What does Bourne Again Shell mean?
Bourne Again Shell is the expanded name of Bash. The Bash man page describes it as a GNU shell that reimplements and extends the Bourne shell (sh), so the name points back to that older shell.
Is Bash a programming language?
Bash is a command language interpreter. It has variables, conditionals, loops and functions, so you can write programs in it, but it is designed mainly to run other programs and connect them together.
Is Bash the same as the terminal?
No. The terminal is the window that displays text. Bash is the program running inside it that reads your commands and starts other programs. You can run a different shell, such as zsh, in the same terminal.
Is Bash the same as sh?
Not exactly. sh is the name of the standard POSIX shell. Bash is built to conform to that standard but adds extra features. When Bash is started under the name sh, it mimics the historical sh startup behaviour and switches to POSIX mode.
Does Windows have Bash?
Windows does not ship Bash as its default shell. You can run it through Git Bash or inside a Linux environment such as WSL.

Sources