>_ IzzyLab

Topic breakdown

Parsing command-line flags by hand via $1, $2 is a path to bugs with ordering and bundled options; for this bash has the built-in getopts, which hands out recognized flags one at a time in a loop. In the spec string, a colon after a flag letter means it takes a mandatory argument, and its value ends up in $OPTARG. Branching by flag name is conveniently done with case, and a flag's requiredness is checked after the loop with a plain test for an empty variable and a corresponding exit code.

What's checked

  1. greet is executable
  2. greet -n Alice prints the greeting
  3. without -n, exit with code 1

What it looks like