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

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 = Environment.GetEnvironmentVariable("FOO");
if (string.IsNullOrEmpty(foo)) foo = "none";
#include <stdlib.h>
const char * foo = getenv("FOO");
if (foo == NULL) foo = "none";
Foo = os:getenv("FOO","none").
  call get_environment_variable ("FOO", length=n, status=st)
  if (st /= 0) then
    foo = "none"
  else
    allocate (character(len=n) :: foo)
    call get_environment_variable ("FOO", value=foo)
  end if 
import "os"
foo, ok := os.LookupEnv("FOO")
if !ok {
	foo = "none"
}
import "os"
foo := os.Getenv("FOO")
if foo == "" {
	foo = "none"
}
sysutils
var
  foo: string;
begin
  foo := GetEnvironmentVariable('FOO');
  if (foo = '') then foo := 'none';
end.
use 5.010;
my $foo = $ENV{FOO} // 'none';
import os
try:
    foo = os.environ['FOO']
except KeyError:
    foo = None
from os import getenv
foo = getenv('FOO')
foo = ENV["FOO"]
imports System
Dim _foo as String
Try
	foo = Environment.GetEnvironmentVariable("FOO")
Catch ex as Exception
	foo = "none"
End Try

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

tkoenig