Skip to main content Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | 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

❗ This post is over one year old. It may no longer be up to date. Opinions may have changed.

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.

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

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