-v VAR (conditional operator)

true if the shell variable VAR is set.

Bash 4 and later also allow to check the value of an array element, e.g. [[ -v myArray[1] ]].

Examples

Different ways to test if a shell variable is set:
1
2
3
4
5
test -v HOME && echo "set"
[ -v HOME ] && echo "set"
# Bash
[[ -v HOME ]] && echo "set"
[[ -v myArray[1] ]] && echo "set"
BashSupport Pro 是一个 支持 -v 的 Bash 集成开发工具 – 马上试用
© 2020–2024 Joachim Ansorg
声明
隐私政策
许可条款