Installing Bash on Linux
Almost every Linux distribution already includes Bash, so in most cases there is nothing to install. This lesson shows how to confirm that Bash is present, how to install or update it with the common package managers when it is missing, how to make it your login shell, and how to run it in a minimal environment such as a container.
Understanding the Concept
On Linux, software normally comes from a package manager, a tool that downloads programs from trusted repositories, installs them in the right places, and tracks their versions. Each distribution family uses its own package manager, and the Bash package is called bash in all of them.
| Distribution family | Examples | Package manager |
|---|---|---|
| Debian-based | Debian, Ubuntu, Linux Mint | apt |
| Red Hat-based | Fedora, RHEL, Rocky Linux, AlmaLinux | dnf (older systems use yum) |
| Arch-based | Arch Linux, Manjaro | pacman |
| SUSE | openSUSE | zypper |
| Alpine | Alpine Linux, many minimal containers | apk |
Installing software needs administrator rights, so the commands below use sudo, which runs one command with elevated privileges. If you are already the root user, as in many containers, omit sudo.
Step 1: Check Whether Bash Is Already Installed
Before installing anything, ask the system where Bash is:
command -v bash
Sample output:
/usr/bin/bash
command -v prints the path of the command that would run, or nothing if the command does not exist. A path means Bash is installed.
You can list every copy that the system can find:
type -a bash
Sample output:
bash is /usr/bin/bash
bash is /bin/bash
On many modern distributions /bin is a link to /usr/bin, so the two entries refer to the same program. You can confirm which real file Bash resolves to:
readlink -f "$(command -v bash)"
Output:
/usr/bin/bash
Finally, check the version that is installed:
bash --version | head -1
Sample output:
GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
The next lessons cover versions in more detail.
Step 2: Check the Package Manager's Record
The package manager keeps a record of what is installed. On Debian-based systems:
dpkg -s bash | grep -E '^(Package|Version)'
Sample output:
Package: bash
Version: 5.2.21-2ubuntu4
The first line confirms the package, and the second shows the exact package version, which includes the distribution's own revision number after the dash. On Red Hat-based systems the equivalent command is rpm -q bash.
Step 3: Install Bash
If command -v bash prints nothing, install it with your package manager. Each command first refreshes or uses the package index, then installs the bash package.
Debian, Ubuntu, and Linux Mint
sudo apt update
sudo apt install bash
apt update refreshes the list of available packages. apt install bash installs Bash, or reports that it is already the newest version.
Fedora, RHEL, Rocky Linux, and AlmaLinux
sudo dnf install bash
On older systems that still use yum, the command is sudo yum install bash.
Arch Linux and Manjaro
sudo pacman -S bash
The -S option tells pacman to synchronize and install the named package.
openSUSE
sudo zypper install bash
Alpine Linux
Alpine images, popular for small containers, use BusyBox ash as the default shell and do not include Bash:
apk add --no-cache bash
The --no-cache option avoids storing the package index, which keeps container images small. After installation, Bash is available at /bin/bash.
Step 4: Update Bash
Bash is updated together with the rest of the system. A normal system upgrade brings in the version that your distribution maintains:
sudo apt update && sudo apt upgrade
This applies to Debian-based systems. Use sudo dnf upgrade on Red Hat-based systems. Distributions generally keep a stable version of Bash and add security fixes to it, so you may see a version number such as 5.2.21-2ubuntu4 where the final part marks the distribution's own patch level, even though the main Bash version stays the same.
To run a newer Bash than your distribution offers, you would build it from source or use a separate package source, but for learning this tutorial the version that ships with a current distribution is enough.
Step 5: Set Bash as Your Login Shell
Installing Bash does not make it your default shell. The login shell is stored in the user database, and you can read it with:
getent passwd "$(id -un)" | cut -d: -f7
Sample output:
/bin/bash
Here id -un prints your user name, getent passwd retrieves that user's record, and cut -d: -f7 extracts the seventh colon-separated field, which is the login shell.
To change it, use chsh:
chsh -s /bin/bash
The -s option names the new shell. The path must be listed in /etc/shells, which you can view with cat /etc/shells. The change takes effect the next time you log in.
Running Bash Without Changing Your Default Shell
You do not have to change your login shell to use Bash. If you are in another shell, start Bash inside it:
bash
A new Bash session starts. Type exit to return to the previous shell. Scripts do not depend on your login shell either, because the shebang line chooses the interpreter for each script.
Installing Bash in a Container
Container images are often minimal, and the shell you get depends on the base image:
- Debian and Ubuntu images include Bash.
- Alpine images do not, and you can add it with
apk add --no-cache bash. - Some very small images contain no shell at all.
To check from outside, run a one-off command in a container:
docker run --rm ubuntu bash --version
The --rm option deletes the container after the command finishes. If the image lacks Bash, the command reports that the executable was not found.
Verifying the Installation
After installing or updating, a short script confirms that everything works:
#!/usr/bin/env bash
echo "Running with Bash $BASH_VERSION"
Save it as hello.sh and run:
bash hello.sh
Output:
Running with Bash 5.2.21(1)-release
If you see the version string, Bash is installed and can run scripts. If the variable is empty, the file was run by another shell.