$OPTARG: Last Argument Value Processed by getopts

$OPTARG contains the value of the last option argument processed by the getopts builtin.

Examples

Print value passed to argument -v:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
usage() { echo "$0 usage: -v <value>"; exit 0; }
[ $# -eq 0 ] && usage
      
declare value
# : because -v requires a value
while getopts "v:" arg; do
  case $arg in
    v) value="$OPTARG" ;;
    *) usage ;;
  esac
done
  
echo "Value passed to -v: $value"
BashSupport Pro is a Bash IDE with support for $OPTARGtry it now!
© 2020–2024 Joachim Ansorg
Imprint
Privacy Policy
Legal