The Short Answer
Bash is a shell: the program that reads the commands you type, runs them, and shows the result. It is the default login shell on most Linux distributions, so when you open an SSH session to a server, you are usually typing into Bash. The name stands for "Bourne Again SHell", a play on the older Bourne shell (sh) that it replaced. Bash is also a programming language: the same commands, saved in a file, make a script.
Shell and Terminal
The two words are often mixed up:
- Terminal: the window that shows text and passes on your keystrokes, such as the terminal application on your computer or an SSH client.
- Shell: the program running inside it that understands the commands.
When you connect with SSH, the terminal is on your computer and the shell runs on the server.
Check Your Shell and Version
echo $SHELL
echo $0
bash --version
$SHELL shows your login shell as set in /etc/passwd, and $0 shows the shell you are in right now. bash --version prints the version on its first line.
Interactive Use
- Completion: press Tab to complete a command, file name, or path.
- History: the up arrow recalls earlier commands, and Ctrl+R searches them; see the history command.
- Pipes:
|sends one command's output into the next, as injournalctl -u nginx | grep error. - Redirection:
>writes output to a file,>>appends to it, and2>redirects error messages. - Variables:
NAME=valuesets one and$NAMEreads it; see environment variables in Linux. - Wildcards:
*.logexpands to every matching file name before the command runs.
Bash Scripts
#!/usr/bin/env bash
set -euo pipefail
for site in /var/www/*/; do
echo "Size of $site: $(du -sh "$site" | cut -f1)"
done
The first line tells the system to run the file with Bash. set -euo pipefail makes the script stop when a command fails (with a few exceptions, such as commands tested by if), when an unset variable is used, or when any part of a pipeline fails. Save it as sizes.sh, make it executable with chmod +x sizes.sh, and run it with ./sizes.sh. The basics are in how to write a Bash script, with loops in the Bash for loop and conditions in the Bash if statement.
Startup Files
Bash reads settings when it starts. A login shell, such as an SSH session, reads /etc/profile and then the first of ~/.bash_profile, ~/.bash_login, or ~/.profile that exists. An interactive shell that is not a login shell reads ~/.bashrc. On Debian and Ubuntu the default ~/.profile loads ~/.bashrc, and on AlmaLinux and Rocky Linux the default ~/.bash_profile does, so aliases and prompt settings belong in ~/.bashrc. See the .bashrc file.
Bash, sh, dash, and zsh
| Shell | Notes |
|---|---|
sh | The POSIX shell; a link to another shell |
dash | Small and fast |
bash | Default login shell on most distributions |
zsh | Extra interactive features |
On Debian and Ubuntu, /bin/sh is dash; on AlmaLinux and Rocky Linux, it is Bash. A script that starts with #!/bin/sh must therefore avoid Bash-only features such as arrays and [[ ]], or it fails on Debian and Ubuntu. A wider comparison is in types of shells in Linux.
Change Your Login Shell
cat /etc/shells
chsh -s /bin/bash
/etc/shells lists the shells allowed as login shells. chsh changes yours; it usually asks for your password, and the change takes effect at the next login. On AlmaLinux and Rocky Linux, chsh comes in the util-linux-user package, which may need to be installed first. An administrator can set another user's shell with sudo usermod -s /bin/bash deploy.