Introduction to Bash
Bash, short for “Bourne Again Shell,” is a command-line interface for Unix and Linux systems and is the default shell for most Linux distributions.
What is a Shell?
Before diving into Bash, it’s important to understand what a shell is. The term “shell” originally means “outer layer,” representing the interface between the user and the kernel. It provides a command-line environment where users interact with the operating system.
In essence, a shell has several meanings:
- Program: The shell is a program that provides an environment for user interaction. This environment typically features a command prompt where users input commands from the keyboard. This setup is also known as the command-line interface (CLI). The shell receives these commands, executes them via the operating system, and returns the results to the user. Unless specified otherwise, “shell” in this context refers to the command-line environment.
- Command Interpreter: The shell interprets user commands. It supports variables, conditionals, loops, and other programming constructs, allowing users to write scripts—small programs that the shell can execute directly without needing compilation.
- Toolbox: The shell provides various utilities that simplify interacting with the operating system.
Types of Shells
There are many types of shells, but any program providing a command-line environment can be considered a shell. Here are some common types of shells:
- Bourne Shell (sh): One of the earliest Unix shells, developed by Stephen Bourne. It provides basic scripting capabilities and is known for its simplicity.
- Bourne Again Shell (bash): An enhanced version of the Bourne Shell, developed by Brian Fox for the GNU Project. It includes features such as command-line editing, job control, and improved scripting capabilities.
- C Shell (csh): Developed by Bill Joy, this shell offers a C-like syntax and features such as job control and history mechanisms.
- TENEX C Shell (tcsh): An enhanced version of the C Shell with additional features like command-line editing, file name completion, and better scripting capabilities.
- Korn Shell (ksh): Developed by David Korn, this shell combines features from the Bourne Shell and C Shell with additional scripting capabilities and performance improvements.
- Z Shell (zsh): Known for its extensive features and customizability, Zsh includes powerful tab completion, better scripting features, and a rich set of plugins and themes.
- Friendly Interactive Shell (fish): A modern shell that focuses on user-friendliness with features like autosuggestions, syntax highlighting, and a user-friendly scripting syntax.
- PowerShell: Developed by Microsoft, this shell is designed for system administration and automation on Windows. It uses cmdlets and offers advanced scripting capabilities.
Bash is currently the most commonly used shell. Unless stated otherwise, “shell” and “Bash” can be used interchangeably.
To check the currently running shell, use:1
2$ echo $SHELL
/bin/bash
To list all shells installed on your Linux system:1
$ cat /etc/shells
In these commands, $
represents the command-line prompt, and you only need to enter the text following it.
Linux allows users to choose different shells, with Bash often being the default or the most compatible option.
Command-Line Environment
Terminal Emulator
In Linux systems without a graphical environment (such as those used for servers), you start directly in the command-line environment.
Most modern Linux distributions, especially those aimed at general users, use graphical environments. After logging in, users are typically in a graphical interface and must launch a terminal emulator to access the command-line environment.
A “terminal emulator” is a program that simulates a command-line window, allowing users to interact with the shell and providing features such as color adjustments, font sizes, and line spacing.
Different Linux distributions and desktop environments offer various terminal programs, such as KDE’s Konsole or GNOME’s gnome-terminal. Despite their different names, all terminal programs essentially provide the same functionality, letting users access the command-line environment and use the shell.
Command-Line Prompt
Upon entering the command-line environment, you’ll see the shell’s prompt, which typically includes a prefix ending with a dollar sign ($). This prompt is where you type your commands.
For example:1
[user@hostname] $
In this prompt, [user@hostname]
is the prefix indicating the username and hostname. For instance, if your username is “johnson” and your hostname is “johnson-machine,” the prompt would look like johnson@johnson-machine $
.
The root user’s prompt ends with a hash sign (#) instead of a dollar sign ($), indicating elevated permissions and reminding users to be cautious. This symbol can be customized; more details can be found in the “Command Prompt” chapter.
For simplicity, the dollar sign ($) will be used to represent the prompt in subsequent examples.
Starting and Exiting Bash
After accessing the command-line environment, you are usually in Bash. If your shell is not Bash, you can start it by entering:1
$ bash
To exit Bash, use the exit
command or press Ctrl + d
:1
$ exit
Basic Bash usage involves typing commands directly at the prompt. For practice, try entering pwd
to display the current directory:1
2$ pwd
/home/me
If you mistakenly type pwe
, you’ll see an error message indicating that the command is not found:1
2$ pwe
bash: pwe: command not found
History of Shells
Shells have evolved alongside Unix systems:
- 1969: Ken Thompson and Dennis Ritchie developed the first Unix version.
- 1971: Ken Thompson created the original shell, known as Thompson shell (sh), for Unix.
- 1973-1975: John R. Mashey enhanced Thompson’s shell by adding programming features, creating the Mashey shell.
- 1976: Stephen Bourne revamped the Mashey shell, resulting in the Bourne shell (sh).
- 1978: Bill Joy developed the C shell (csh), introducing C-like syntax.
- 1979: Unix Version 7 included the Bourne shell, making it the default for Unix systems.
- 1983: David Korn created the Korn shell (ksh).
- 1985: Richard Stallman founded the Free Software Foundation (FSF) and began developing a free shell to avoid licensing issues with Unix. This led to the creation of Bash.
- 1988: Brian Fox, the FSF’s first paid programmer, released Bash, a free software clone of the Bourne shell.
- 1989: Bash 1.0 was released.
- 1996: Bash 2.0 was released.
- 2004: Bash 3.0 was released.
- 2009: Bash 4.0 was released.
- 2019: Bash 5.0 was released.
To check your local Bash version, use:1
2
3
4
5
6
7$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Or:1
2$ echo $BASH_VERSION
5.1.16(1)-release