假设您有一个带有许多选项的函数,而您决定动态存储它们的值:
#!/bin/bash
fun() {
local OPTIND __option
while getopts ":$(printf '%s:' {a..z} {A..Z})" __option
do
case $__option in
[[:alpha:]])
# ...
local "__$__option=$OPTARG"
esac
done
# here you might get a conflict with external variables:
[[ ${__a:+1} ]] && echo "option a is set"
[[ ${__b:+1} ]] && echo "option b is set"
# etc...
[[ ${__Z:+1} ]] && echo "option Z is set"
}
有没有一种方法可以检查变量是否在本地定义?
bash
手册中:如果没有操作数,local
将本地变量列表写入标准输出。 - Renaud Pacalet