Arg Parsing in Bash
I was writing a script the other day that I needed to accept some optional
arguments. I found that I could use the getopt
command, but turns out the
simple approach was cleaner in the end.
#!/usr/bin/env bash
while [[ $# -gt 0 ]]; do
case "$1" in
-c | --config)
config="$2"
shift 2
;;
-u | --url)
url="$2"
shift 2
;;
*)
args="$1"
shift
;;
esac
done
echo "$config"
echo "$url"
echo "$args"