Skip to content

Instantly share code, notes, and snippets.

@EuphoryX1
Last active December 12, 2015 03:09
Show Gist options
  • Save EuphoryX1/4705038 to your computer and use it in GitHub Desktop.
Save EuphoryX1/4705038 to your computer and use it in GitHub Desktop.
#!/bin/bash
while getopts a:bc: flag; do
case $flag in
a)
opt_a=$OPTARG
;;
b)
opt_b=1
;;
c)
opt_c=$OPTARG
;;
?)
echo "usage: ...."
exit 1
;;
esac
done
shift $(( OPTIND - 1 ));
echo $*
##
## bash -x ~/work/bash_test/test_getopts.sh -a 1 -b -c a arg1 arg2 result below
+ getopts a:bc: flag
+ case $flag in
+ opt_a=1
+ getopts a:bc: flag
+ case $flag in
+ opt_b=1
+ getopts a:bc: flag
+ case $flag in
+ opt_c=a
+ getopts a:bc: flag
+ shift 5
+ echo arg1 arg2
arg1 arg2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment