Environment variables

From Rosetta Code
Revision as of 09:56, 23 December 2008 by rosettacode>Spoon! (started new task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Environment variables
You are encouraged to solve this task according to the task description, using any language you may know.

Show how to get one of your process's environment variables. The available variables vary by system; some of the common ones available on Unix include PATH, HOME, USER.

C

<c>#include <stdlib.h>

  1. include <stdio.h>

int main() {

 puts(getenv("HOME"));
 return 0;

}</c>

Haskell

import System.Environment
main = do getEnv "HOME" >>= print  -- get env var
          getEnvironment >>= print -- get the entire environment as a list of (key, value) pairs

Java

<java>System.getenv("HOME") // get env var System.getenv() // get the entire environment as a Map of keys to values</java>

OCaml

<ocaml>Sys.getenv "HOME"</ocaml>

Perl

The %ENV hash maps environmental variable names to their values: <perl>$ENV{'HOME'}</perl>

PHP

The $_ENV associative array maps environmental variable names to their values: <php>$_ENV['HOME']</php>

Python

The os.environ dictionary maps environmental variable names to their values: <python>import os os.environ['HOME']</python>

Ruby

The ENV hash maps environmental variable names to their values: ENV['HOME']