Installing Bash on Linux

Su Suriya Ravichandran Updated 06 Dec 2026
5 min read ·Lesson 7 of 205

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 familyExamplesPackage manager
Debian-basedDebian, Ubuntu, Linux Mintapt
Red Hat-basedFedora, RHEL, Rocky Linux, AlmaLinuxdnf (older systems use yum)
Arch-basedArch Linux, Manjaropacman
SUSEopenSUSEzypper
AlpineAlpine Linux, many minimal containersapk

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.

0 Comments

Reviewed before they appear

No comments yet.

Bash
Introduction to Bash What Is a Shell? What Is Bash (Bourne Again Shell)? Bash vs. Shell vs. Terminal Bash vs. sh, Zsh, and Fish Where Bash Is Used: Linux, DevOps, and Automation Installing Bash on Linux Using Bash on macOS Using Bash on Windows with WSL Checking Your Bash Version Opening and Using a Terminal Running Your First Bash Command Understanding Command Syntax and Arguments Bash Built-in Commands vs. External Commands Working with pwd, ls, and cd Creating Files and Directories with touch and mkdir Copying, Moving, and Renaming Files Removing Files and Directories Safely Viewing Files with cat, less, head, and tail Searching for Files with find Searching Text with grep Understanding File Paths and Wildcards Using man Pages and Command Help Bash Script Structure Creating Your First Bash Script Understanding the Shebang (#!/usr/bin/env bash) Making a Script Executable with chmod Running a Bash Script in Different Ways Writing Comments in Bash Using echo and printf Reading User Input with read Displaying Helpful Messages in Scripts Using Semicolons and Newlines Bash Variables: Declaration and Usage Rules for Naming Bash Variables Environment Variables Local Variables vs. Environment Variables Exporting Variables with export Common Environment Variables: HOME, PATH, USER, and PWD Read-Only Variables with readonly Removing Variables with unset Command-Line Arguments: $0, $1, and More Special Parameters: $#, $?, $$, $!, $@, and $* Positional Parameters and shift Default Values with Parameter Expansion Single Quotes vs. Double Quotes Escaping Special Characters Command Substitution with $(...) Arithmetic Expansion with $((...)) Brace Expansion Tilde Expansion and Home Directories Filename Expansion and Globbing Understanding Word Splitting Parameter Expansion Basics Why Quoting Variables Matters Standard Input, Standard Output, and Standard Error Redirecting Output with > and >> Redirecting Input with < Redirecting Errors with 2> Combining Standard Output and Standard Error Using Pipes (|) to Connect Commands Using tee to Save and Display Output Here Documents with << Here Strings with <<< Command Lists with ;, &&, and || Conditional Statements with if, then, and fi Using elif and else Testing Conditions with test and [ ] Using [[ ]] for Bash Conditionals Comparing Strings in Bash Checking Whether Files Exist Checking File Types and Permissions Integer Comparisons in Bash Logical Operators: &&, ||, and ! Using case Statements Pattern Matching in case Common Conditional Statement Mistakes for Loops in Bash C-Style for Loops while Loops until Loops Looping Through Files and Directories Looping Through Command Output Safely break and continue Nested Loops select Loops for Simple Menus Avoiding Infinite Loops Defining and Calling Bash Functions Passing Arguments to Functions Returning Status Codes from Functions Understanding return vs. echo in Functions Local Variables in Functions Function Scope and Variable Visibility Organizing Scripts into Functions Sourcing Other Bash Files with source Reusable Bash Utility Functions Indexed Arrays in Bash Creating and Accessing Array Elements Adding and Removing Array Elements Looping Through Arrays Array Length and Indices Associative Arrays in Bash Iterating Over Associative Arrays Passing Arrays to Functions Common Array Pitfalls Processing Text with grep, sed, and awk Using cut, sort, uniq, and wc Finding and Replacing Text with sed Extracting Columns with awk Parsing CSV-Like Text from the Command Line Working with Filenames That Contain Spaces Safe File Handling with find -print0 and xargs -0 Temporary Files with mktemp Reading Files Line by Line Appending Logs to a File Understanding Exit Codes Checking Command Success with $? Using exit to End a Script Fail-Fast Scripts with set -e: Benefits and Caveats Using set -u and set -o pipefail Combining Strict Mode Options Safely Handling Errors with if Statements Traps with trap Cleaning Up Temporary Files on Exit Writing Useful Error Messages Debugging Scripts with bash -x Parsing Script Arguments Validating Required Arguments Handling Optional Arguments Parsing Options with getopts Building Help Messages with --help Setting Defaults for Optional Parameters Handling Unknown Options and Invalid Input Creating Interactive Menus in Bash Foreground and Background Processes Running Commands in the Background with & Managing Jobs with jobs, fg, and bg Waiting for Processes with wait Capturing Process IDs with $! and $$ Sending Signals with kill Handling Interrupts and Termination Signals Running Commands with timeout Basic Process Monitoring from Bash Aliases and Shell Functions: When to Use Each Customizing the Bash Prompt (PS1) Bash Startup Files: .bashrc, .bash_profile, and Profile Interactive vs. Non-Interactive Bash Shell Options with set shopt: Enabling Bash-Specific Options Directory Stack with pushd, popd, and dirs Process Substitution with <(...) and >(...) Advanced Parameter Expansion Pattern Matching and Extended Globbing Bash Script Security Fundamentals Avoiding Command Injection in Shell Scripts Quoting User Input Safely Why eval Is Risky and Safer Alternatives Handling Secrets and Credentials in Scripts Using Least Privilege in Automation Scripts Safe Use of rm and Destructive Commands Validating Paths and User-Supplied Filenames ShellCheck: Static Analysis for Bash Scripts Writing Readable and Maintainable Bash Automating Backups with Bash Creating Log Rotation and Cleanup Scripts Checking Disk Usage and Free Space Monitoring CPU, Memory, and System Load Checking Website Availability with curl Automating Updates on Ubuntu and Debian Scheduling Bash Scripts with cron Using Lock Files to Prevent Duplicate Runs Building a Simple Health-Check Script Sending Notifications from Bash Scripts Using Bash in Git Workflows Bash Scripts for Docker Automation Automating Docker Container Health Checks Using Bash in CI/CD Pipelines Environment Configuration for Deployment Scripts Automating SSH Tasks with Bash Deploying a Web Application with a Bash Script Using Bash to Process Application Logs Building Repeatable Development Setup Scripts Bash Debugging Techniques Using set -x and PS4 for Debug Traces Testing Bash Scripts with Bats Mocking Commands During Script Tests Testing Error Paths and Exit Codes Writing Portable Shell Scripts Bash-Specific Syntax vs. POSIX Shell Syntax Using ShellCheck in a Development Workflow Performance Tips for Bash Scripts Build an Interactive Calculator Build a File and Directory Organizer Build a Timestamped Backup Script Build a Log File Analyzer Build a Bulk File Renaming Tool Build a System Resource Monitoring Script Build a Website Uptime Checker Build a User Account Audit Report Build a Database Backup Automation Script Build a Docker Cleanup Helper Build a Server Health-Check and Alert Script Build a Deployment Automation Script Build a Menu-Driven Linux Administration Toolkit Build a Safe Automated Backup and Restore Workflow
Ask about this post
AI Ask about this post

Ask questions about Installing Bash on Linux and get answers drawn from it.

Signed-in readers only.