Skip to content

Instantly share code, notes, and snippets.

@pda
Created March 5, 2014 02:35
Show Gist options
  • Select an option

  • Save pda/9360141 to your computer and use it in GitHub Desktop.

Select an option

Save pda/9360141 to your computer and use it in GitHub Desktop.
getopts (bash) usage example.
#!/bin/bash
# Example: ./getopts.sh -b world -a hello one two three
# Output: A=hello B=world 1=one 2=two 3=three 4=
while getopts "a:b:" opt; do
case "$opt" in
"a") A="$OPTARG";;
"b") B="$OPTARG";;
esac
done
shift $(( OPTIND - 1 ))
echo "A=$A B=$B 1=$1 2=$2 3=$3 4=$4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment