-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 is a Bash IDE with support for -vtry it now!
© 2020–2024 Joachim Ansorg
Imprint
Privacy Policy
Legal