Skip to main content

Bash parameter parsing template.

#!/usr/bin/env bash

dohelp()
{
    echo "Example script"
    echo ""

    # Exit because you don't want the script to do anything after displaying help
    exit
}

while [ -n "$*" ]; do
    flag=$1
    value=$2

    case "$flag" in
        -o|--one)
            one=$value
            shift
        ;;
        -t|--two)
            two=$value
            shift
        ;;
        -p|--pretend)
            pretend=true
        ;;
        -h|--help)
            dohelp
        ;;
        --)
            break
        ;;
        *)
            echo -e "unknown option $flag\n"
            dohelp
        ;;
  esac

  shift
done