Skip to main content

Bash Shell Script Template

Published: 2020-12-08

Bash is one of my favorite scripting languages. I often find myself doing a task many times, only to turn it into a shell script for easy usage.

One area that Bash really shines is “gluing” tools together. So, I usually have to check for the existence of tools before running the script body. Say, for example, my script depended on git and python. I would add them as a space-separated-array like REQUIRED_TOOLS=(git python). Then that array is iterated and each tool is checked for existence. If it doesn’t exist, the script logs an error message and exits.

After the script runs, it’s nice to know how long it took. This is done by logging the SECONDS Bash variable.

Happy scripting.

#!/usr/bin/env bash


for tool in ${REQUIRED_TOOLS[@]}; do
  if ! command -v ${tool} >/dev/null; then
    echo "Tool ${tool} is required. Exiting"; exit 1

# Content goes here

echo "Completed in ${SECONDS}s"