$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 是一个 支持 $OPTARG 的 Bash 集成开发工具 – 马上试用
© 2020–2024 Joachim Ansorg
声明
隐私政策
许可条款