Logo

Programming-Idioms

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

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

import os
foo = os.environ.get('FOO', 'none')

Python exposes environment variables as an associative array(dict), so the standard get method can be used.
import os
try:
    foo = os.environ['FOO']
except KeyError:
    foo = "none"
from os import getenv
foo = getenv('FOO', 'none')
with Ada.Environment_Variables;
Foo : constant String :=
      Ada.Environment_Variables.Value (Name    => "FOO",
                                       Default => "none");

New implementation...
< >
tkoenig