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

Check if "the parameter named by the value of x" is set

我目前正在使用这样一段代码:

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

有没有更优雅或更简洁的表达方式?本来我试过

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

但这引发了“错误替换”错误。

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

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

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

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

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