Skip to main content Logo (IEC resistor symbol) logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Bash Bits: Check if a program is installed

Published: 05-05-2019 | Author: Remy van Elst | Text only version of this article

Table of Contents

Bash Bits are small examples and tips for Bash Scripts. This bash bit shows you how to check if a piece of software is installed on a machine. It's a function you can use in your shell scripts.

If you like this article, consider sponsoring me by trying out a Digital Ocean VPS. With this link you'll get $100 credit for 60 days). (referral link)

All Bash Bits can be found using this link


The function uses the command binary to check if a binary is available. command handles all the magic of searching your path, checking if something is executable, all the dificult stuff that could also be different on other distributions.

This is the function:

command_exists() {
    # check if command exists and fail otherwise
    command -v "$1" >/dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo "I require $1 but it's not installed. Abort."
        exit 1

You can use it in a loop to check multiple binaries:

for COMMAND in "awk" "sed" "grep" "tar" "gzip" "which" "openssl" "curl"; do
    command_exists "${COMMAND}"

If a command does not exist, your script will fail and exit with an error message:

I require curl but it's not installed. Abort.
Tags: bash , bash-bits , binary , command , installed , manpage , shell , snippets