This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
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++
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Go
- Go
- Groovy
- Haskell
- JS
- JS
- Java
- Java
- Kotlin
- Lisp
- 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
string foo = Environment.GetEnvironmentVariable("FOO");
if (string.IsNullOrEmpty(foo)) foo = "none";
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."
(defvar foo
(uiop:getenv "FOO")
"The contents of environment variable FOO.")
Common Lisp doesn't have a built-in way to examine environment variables, so we rely on the portability library UIOP. I believe all currently maintained implementations include UIOP as part of ASDF. The REQUIRE form loads ASDF, and thus UIOP.
If an environment variable is present and empty then FOO will be an empty string. "FOO" is not present on my system, so FOO gets NIL.
If an environment variable is present and empty then FOO will be an empty string. "FOO" is not present on my system, so FOO gets NIL.
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