I need to check a variable's existence in an if statement. Some kind of analogy

if [ -v $somevar ]
then
    echo "Variable somevar exists!"
else
    echo "Variable somevar does not exist!"

And the closest question to that was this, which doesn't actually answer my question.

Best Answer


In modern bash (version 4.2 and above).

[[ -v name_of_var ]]

From help test .

-v VAR, True if the shell variable VAR is set