This expression
sys.env("HOME")
delivers the path to a home folder; in general sys.env("EnvVar")
delivers the value associated to $EnvVar
. However, for an environment variable $LIB
not declared
java.util.NoSuchElementException: key not found: LIB
How to find out whether the variable is declared without having to catch an exception ?
sys.env
is a Map
, so you can call its get
method, which returns an Option
:
scala> sys.env.get("FOO")
res0: Option[String] = None
You can try this:
val value = sys.env.get("EnvVar")
println(value.getOrElse("Your default value"))
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