Skip to content

Instantly share code, notes, and snippets.

@whitekid
Last active January 26, 2017 01:02
Show Gist options
  • Save whitekid/72ce0e1f637eae6ae0bfdc77bdcce14b to your computer and use it in GitHub Desktop.
Save whitekid/72ce0e1f637eae6ae0bfdc77bdcce14b to your computer and use it in GitHub Desktop.
bash extra argement(--) parsing with getopts
#!/bin/bash
#
# extra_args.sh -l args script_args -- -l extra args
#
# OUTPUT
# opt: l=args
# arg: %script_args%
# extra_args: %-l extra args%
#
while getopts 'l:' opt; do
case $opt in
l)
echo "opt: $opt=$OPTARG";;
esac
done
shift $((OPTIND-1))
while [[ $# -gt 0 ]]; do
case $1 in
--)
shift;
extra_args="$@"
break;;
*)
args+="$1 ";;
esac
shift
done
args="${args/% /}"
echo "arg: %$args%"
echo "extra_args: %$extra_args%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment