检查是否设置了"the parameter named by the value of x"



我目前使用的代码如下:

if (( $( eval "echo ${+$foo}" ) )); then
./my-prog
fi

有没有更优雅或简洁的方式来表达这一点?最初我尝试了

if (( ${+${(P)foo}} )); then
./my-prog
fi

但这引发了";糟糕的替换";错误

还有-v运算符,它排除了间接参数扩展的需要:

if [[ -v $foo ]]; then
./my-prog
fi

这在条件表达式部分的man zshmisc中有记录。

(P)+可以在相同的参数扩展中使用,如果您按正确的顺序使用它们:

if (( ${(P)+foo} )); then
./my-prog
fi

相关内容

最新更新