Skip to content

Instantly share code, notes, and snippets.

@c-garcia
Created January 5, 2017 08:49
Show Gist options
  • Save c-garcia/95e488e974f207f3afa95fca2fdf683b to your computer and use it in GitHub Desktop.
Save c-garcia/95e488e974f207f3afa95fca2fdf683b to your computer and use it in GitHub Desktop.
bash getopts example
#!/bin/bash
# first colon turns getopts into silent mode
# that also sets OPTARG when errors happen
#
# options with arguments have a colon AFTER the
# option letter
# in the example, a requires an option, b no
while getopts ":a:b" opt; do
case $opt in
a)
echo "a with $OPTARG"
;;
b)
echo "b was set"
;;
\?)
echo "INVALID OPTION: $OPTARG"
;;
\:)
echo "INVALID ARGUMENT: $OPTARG"
;;
esac
done
shift $((OPTIND - 1))
echo "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment