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
In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable you set earlier. For example, to check if MARI_CACHE is set, enter echo %MARI_CACHE%. If the variable is set, its value is displayed in the command window.
Vim can read and write environment variables within your current shell session. Use a $ prefix to identify an environment variable, as in the following examples. Insert the contents of the PATH environment variable into the buffer: Press i to enter insert mode, press Ctrl-r then =$PATH and press Enter.
To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With