elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.67k stars 299 forks source link

Test variable is set #845

Closed ktm5j closed 5 years ago

ktm5j commented 5 years ago

Can someone tell me the correct way to test if a variable has been set? Like how in bash you do if [ -n "$VAR" ]

Everything I try leads to a runtime failure saying Compilation error: variable $VAR not found, even if I wrap it in a try block.

Thanks!

xiaq commented 5 years ago

Variables in Elvish are lexically scoped, so it is not possible to refer to a nonexistent variable. This is intentional: https://elv.sh/ref/language.html#scoping-rule

What is your use case for this?

ktm5j commented 5 years ago

I'm trying to duplicate the functionality of environment modules (http://modules.sourceforge.net/).. but I'm now realizing that I can use has-env from the builtin module for what I need. Thanks for the info!