Skip to main content
  1. Blogs/

Bash Function for Checking Installed Applications

·3 mins
Table of Contents
Telling your future self exactly where to find that tool you need is always a good idea.

Tools
Photo by Lachlan Donald on Unsplash

I often find myself writing small bash scripts for doing repetitive tasks on various *nix machines where I may not 100% know which applications are installed. I’ve come up with a bash function to solve this problem that I thought was worth sharing on a quick post.

This works especially well for internally developed applications where the latest version needs to be hunted down every time and allows me to get whatever application I need installed quickly from a known trusted location.

The function has also proved useful for adding to shared scripts within my team. It helps notify people that the script will not work before hitting errors for missing installed tools and it allows me to show where I’ve sourced the required applications that they need in order to use the script.

check_installed() {
  required_applications=( "terraform" "vault" "jq" "franks_tool" )

  app_link_terraform="https://terraform.io/downloads"
  app_link_vault="https://vaultproject.io/downloads"
  app_link_jq="https://stedolan.github.io/jq/download"
  app_link_franks_tool="Ask Frank for the latest binary"

  missing_applications=()
  for app in "${required_applications[@]}"; do
    if ! command -v "$app" &> /dev/null; then
      missing_applications+=("$app")
    fi
  done

  if [ ${#missing_applications[@]} -gt 0 ]; then
    echo -e "Required applications to use this script are not currently installed.\n"
    echo "Please install the following before running the script again:"
    for app in "${missing_applications[@]}"; do
      link="app_link_$app"
      echo "  - $app, ${!link}"
    done
    exit
  else
    echo "Required applications are installed."
  fi
}

Usage #

To use this function, just copy it into any bash script and add check_installed to the main flow. Edit the the expandable list of required applications (required_applications) and where to find them (app_link_appname).

Be sure to match the exact appname in the app_link_x variable since this is used to assign the application to a specific message for getting the app installed.

How it works #

After some Googling/Stack Overflow searching on the best way to determine whether a CLI application is installed, I settled on using the Bash Builtin command since this will return whether an application is found on the PATH and establish if the application would be available for bash to run.

Using the -v option will only return a description of command rather than executing the application itself, which could be unhelpful if executing the application is not yet intended. Using this option for a present application will return a status of zero if the command is found and non-zero if it is not. This allows for using this bash builtin in a simple if statement to determine whether an application is available to bash.

As noted on the Stack Overflow answer, this is also POSIX compatible way of doing it and should be standard across different *nix environments with different Unix shells (e.g. sh, bash, zsh, fish).