Bash programming/Parsing Command-Line Arguments

Parsing command-line argument can be done at least using three different methods[1]

  • Using getopt[s]
  • Bash Space-Separated (e.g., --option argument) (without getopt[s])
  • Bash Equals-Separated (e.g., --option=argument) (without getopt[s])


Basic usage template:

#!/bin/bash

program_name=$0

function usage {
    echo "Usage: $program_name [parameter1] [parameter2] [parameter3]"
    echo "  parameter1 - parameter 1 explanation"
    echo "  parameter2 - parameter 2 explanation"
    echo "  Example: $program_name parameter1 parameter2 parameter3"
    exit 1
}

if [ $# == 0 ]; then
 usage
fi

See also

edit
  1. https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash