Skip to content

Instantly share code, notes, and snippets.

@versionsix
Created June 29, 2018 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save versionsix/7a936051b4ecb175a8331fb59d428ac2 to your computer and use it in GitHub Desktop.
Save versionsix/7a936051b4ecb175a8331fb59d428ac2 to your computer and use it in GitHub Desktop.
Bash: Argument Parsing
#!/bin/bash
PARAMS=""
while (( "$#" )); do
case "$1" in
-1|--flag-with-argument-1)
F_ARG_1=$2
shift 2
;;
-2|--flag-with-argument-2)
F_ARG_2=$2
shift 2
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
echo $F_ARG_1
echo $F_ARG_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment