conditional-operator

Bash -v — variable is set

Tests whether the named variable has an assigned value, including an empty string. Use trusted scalar names; array subscripts have additional rules and evaluation risks. This example uses Bash 5.2 and scalar variables only. Run the example independently in a fresh private directory. Expected output (one item per line): unset=1; empty=0.

Formal Syntax & Structure

bash syntaxbash
[[ -v name ]]

Parameters & Attributes

NameTypeDescription
namevariable nameTrusted shell variable name, not its expanded value.

Return Value

exit status

0 for a true condition; 1 for a false condition. Invalid expressions may fail differently.

Working Example

Example Snippetbash
unset value[[ -v value ]]; printf 'unset=%s\n' "$?"value=[[ -v value ]]; printf 'empty=%s\n' "$?"
Bash Reference