What Is Bash (Bourne Again Shell)?
Bash is a command-line shell and scripting language. Its name stands for Bourne Again Shell, a pun on the original Bourne shell (sh) that it was designed to replace. Brian Fox wrote the first version for the GNU Project, and it was released in 1989 as the shell of the GNU operating system. It is still maintained by the GNU Project, and it became the default shell of most Linux distributions.
Understanding the Concept
Bash does three jobs at once:
- An interactive shell. It offers a prompt, command history, line editing, and tab completion for people working in a terminal.
- A scripting language. It provides variables, conditions, loops, functions, and arrays for writing automation.
- A host for other programs. It starts programs, connects them with pipes and redirections, and manages them as jobs.
Bash was designed to be compatible with the Bourne shell. Most scripts written for sh also run under Bash. At the same time, Bash adds many features that the original shell did not have, which is the "Again" in the name: it takes the Bourne shell idea and goes further.
Features Added Beyond the Bourne Shell
Bash introduced or adopted several features that make both typing and scripting easier.
Command History and Line Editing
Bash remembers commands you ran and lets you recall and edit them with the arrow keys. The history command lists them. Pressing Ctrl+R searches backward through earlier commands.
Tab Completion
Pressing the Tab key completes command names, file names, and in many cases options. This saves typing and reduces mistakes.
Arrays
Bash supports indexed arrays and associative arrays (key and value pairs), which the original Bourne shell lacked:
names=(Asha Ravi Meena)
echo "${names[1]}"
echo "${#names[@]}"
Output:
Ravi
3
The first line creates an array with three elements. Array positions start at zero, so ${names[1]} is the second element, Ravi. The expression ${#names[@]} gives the number of elements.
Extended Conditional Test
Bash offers the [[ ... ]] test, which is more powerful and safer than the older [ ... ] form:
file="report.txt"
if [[ $file == *.txt ]]; then
echo "This is a text file"
fi
Output:
This is a text file
Inside [[ ]], the right-hand side of == is treated as a pattern, so *.txt matches any name that ends in .txt.
Arithmetic
Bash can calculate integers directly with arithmetic expansion:
echo $((7 * 6))
Output:
42
The $(( ... )) form evaluates the expression inside and substitutes the result.
Process Substitution, Brace Expansion, and More
Bash adds convenient expansions such as {1..5}, which produces the numbers one to five:
echo {1..5}
Output:
1 2 3 4 5
The shell expands the brace pattern into five separate words before echo runs.
Checking That You Are Using Bash
Because Bash has features other shells lack, scripts often need to know which shell is running. Bash stores its version in the variable BASH_VERSION:
echo "$BASH_VERSION"
echo "${BASH_VERSINFO[0]}"
Output on a system with Bash 5.2:
5.2.21(1)-release
5
BASH_VERSION is a string with the full version. BASH_VERSINFO is an array whose first element, index zero, is the major version number. If a script is run by a shell that is not Bash, BASH_VERSION is empty, which makes it a useful check.
Bash as a Program
Bash is an executable program, typically stored at /bin/bash or /usr/bin/bash. You can start it directly to open a new shell inside your current one, or give it a file to run:
bash script.sh
Here, bash is the program and script.sh is its argument. Bash reads the file and executes the commands inside it. To print only the version of the installed Bash, use:
bash --version | head -1
Sample output:
GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
The pipe to head -1 keeps only the first line, because the full output also contains licensing text.
The GNU Project and Licensing
Bash is free software released under the GNU General Public License. That means anyone can read its source code, change it, and share it under the same terms. This openness is a major reason Bash spread so widely: Linux distributions could include it freely, and users could rely on it behaving the same way across systems.
Where Bash Fits in the Larger Picture
Bash is not the only shell, and it is not the oldest. It sits in the middle of a family tree of shells:
- The Bourne shell (
sh) came first and set the basic syntax. - Bash extended it with arrays, better interactivity, and many conveniences.
- Other shells such as Zsh and Ksh also built on the same ideas.
- POSIX, a standard for portable shell scripting, describes the common subset that many shells agree on.
When you write a script with Bash-specific features, you can only rely on it running where Bash is installed. When you stick to the POSIX subset, the script is portable to more shells. A later lesson compares the two approaches in detail.
Bash on Different Systems
Bash is available almost everywhere:
- Linux: installed by default on most distributions.
- macOS: includes an old Bash 3.2 for licensing reasons, while newer versions can be installed separately.
- Windows: available through the Windows Subsystem for Linux or through tools such as Git Bash.
Because versions differ, features added in newer releases, such as some associative array behavior, may not exist on older systems. The lesson on checking your Bash version shows how to handle this.