In code development, be it one or the other requirement, we need to access the environment variable. In this post, the environment variable will be read/set in 5 different programming languages.

  1. C language

    In C, environment variables can be read and set by getenv() and putevn() methods respectively. The example code is shown below.

    #include
    #include
    
    void print_env(char * env_var){
    
      char *path = getenv (env_var);
    
      if (path!=NULL){
        printf (" %s \n",path);
      }
      else{
        printf(" %s not found in the environment\n", env_var);
      }
    }
    int main ()
    {
      print_env("HOME");
      putenv("C_ENV_SET=C set the env");
      print_env("C_ENV_SET");
      return 0;
    }
    
    
  2. Java Language

    Java provides System.getenv() method to read the environment variable but it doesn’t provide method to set the environment variable. Java asserts this kind of operation as insecure and hence JVM doesn’t allow such operation. But it can be achieved by calling native methods to set the environment variable using JNI. If you want to store some values and access it later, then  consider System.setProperty() and Sytem.getProperty() as an option. The following code shows how to read the system variables.

    import java.util.Map;
    
    public class EnvRead {
        public static void main (String[] args) {
            Map env = System.getenv();
             System.out.println(env.get("HOME"));
    
        }
    }
    
  3. Shell scripting

    It’s really straight forward to read and set the environment variable in Shell script. It uses echo and export command.

    #!/bin/sh
    
    #Read the environment variable.
    echo $HOME
    
    #Set the environment variable
    export SH_ENV_SET='export set the env'
    
    #read the Set environment variable.
    echo $SH_ENV_SET
    
  4. Python script

    Python provides OS module which has environ mapping object representing the string environment. The following code is to access and set the environment variable.

    #!/usr/bin/env python
    
    import os
    
    #For accessing the environment variable.
    print os.environ['HOME']
    
    #For setting the environment variable.
    os.environ['PY_ENV_SET']='environ set the env'
    
    print os.environ['PY_ENV_SET']
    
  5. Gradle scripting

    Sometimes even in build script we need to access environment variables. Accessing environment variable is easy and it is shown  below. Gradle is similar to java and runs in JVM and hence it also has similar restrictions as that of Java for setting environment variable.

    task env_read<<{
    
      println "$System.env.HOME"
      //Other way of accessing the environment variable.
      println System.getenv()['HOME']
    }
    

Feel free to comment on the post or the website.
Thanks!!

, , ,
Trackback

only 1 comment untill now

  1. Nikos Maravitsas @ 2012-08-20 03:48

    Hi ,

    Great blog! Is there an email address I could contact you in private?

Add your comment now