Reputation: 5082
How to evaluate if an bash environment variable is set
for example
function! Myfoo(arg)
if $SomeVar is set/exist ----> how to eval the SomeVar has been set
....
endif
endfunction
Upvotes: 18
Views: 6557
Reputation: 172590
You've (intuitively?) used the correct syntax; as :help expression-syntax
explains (under :help expr-env
), the syntax is $VAR
.
You can compare with an empty string (if $SomeVar != ""
) or use empty()
(if !empty($SomeVar)
) to check whether a (non-empty) value has been supplied. It's not so easy to differentiate between empty environment variable and non-existing environment variable, so this is best avoided. (This distinction also is rarely used in shell scripts itself, neither.)
Example:
# vi ~/.vimrc
if !empty($vim_background)
:let &background = $vim_background
endif
Upvotes: 27