Skip to content

Instantly share code, notes, and snippets.

@nikvdp
Last active October 17, 2021 09:57
Show Gist options
  • Save nikvdp/5e23113c842dd220865cacb8cbaa0871 to your computer and use it in GitHub Desktop.
Save nikvdp/5e23113c842dd220865cacb8cbaa0871 to your computer and use it in GitHub Desktop.
rest=()
while [[ $# -ge 1 ]]; do
arg="$1"
shift
case "$arg" in
-f|--foo)
foo=true
;;
-p|--param-that-takes-an-arg)
params_arg="$1"
shift # extra shift so we don't re-parse the param's arg
;;
*)
# optionally keep unknown options around to be handled later
rest=("${rest[@]}" "$arg")
;;
esac
done
set -- "${rest[@]}" "$1" # let anything we didn't parse out above be
echo "foo: $foo"
echo "arg for -p: $params_arg"
echo "Unparsed args: $@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment