Idiom #205 Get an environment variable
Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".
- Ada
- C
- Clojure
- C++
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Go
- Groovy
- Haskell
- JS
- JS
- Java
- Java
- Kotlin
- Lua
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Rust
- Rust
- Rust
- Rust
- Scala
- Smalltalk
- VB
- VB
Foo : constant String :=
Ada.Environment_Variables.Value (Name => "FOO",
Default => "none");
const char * foo = getenv("FOO");
if (foo == NULL) foo = "none";
Returns a pointer to the value or NULL
const char* tmp = std::getenv("FOO");
std::string foo = tmp ? std::string(tmp) : "none";
If environment variable was not found, std::getenv returns nullptr
do
foo <- catch (getEnv "FOO") (const $ pure "none" :: IOException -> IO String)
-- do something with foo
const foo = process.env.FOO ?? 'none'
The nullish colaescing operator (??) is available in ES2020 onwards. It ensures that if the FOO environment variable is the empty string, foo isn't set to 'none'.
String foo;
try {
foo = getenv("FOO");
if (foo == null) throw new Exception();
} catch (Exception e) {
foo = "none";
}
"... On UNIX systems the alphabetic case of name is typically significant, while on Microsoft Windows systems it is typically not."
foo = IIf(Environ("FOO") = "", "none", Environ("FOO"))
https://docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/iif-function
Dim _foo as String
Try
foo = Environment.GetEnvironmentVariable("FOO")
Catch ex as Exception
foo = "none"
End Try