Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java

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".

String foo = System.getenv("foo");
if (foo == null) {
	foo = "none";
}
import static java.lang.System.getenv;
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."
with Ada.Environment_Variables;
Foo : constant String :=
      Ada.Environment_Variables.Value (Name    => "FOO",
                                       Default => "none");

New implementation...
< >
tkoenig