{"id":8,"date":"2012-09-12T04:46:51","date_gmt":"2012-09-12T04:46:51","guid":{"rendered":"https:\/\/code4reference.com\/?p=8"},"modified":"2012-09-12T04:46:51","modified_gmt":"2012-09-12T04:46:51","slug":"code4referencehow-to-read-environment-variable-in-5-different-languages","status":"publish","type":"post","link":"https:\/\/code4reference.com\/?p=8","title":{"rendered":"Code4ReferenceHow to read environment variable in 5 different languages."},"content":{"rendered":"<h2><a href=\"http:\/\/code4reference.com\/2012\/08\/read-environment-variable\/\">How to read environment variable in 5 different languages?<\/a><\/h2>\n<p>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.<\/p>\n<ol>\n<li>\n<p>In C, environment variables can be read and set by getenv() and putevn() methods respectively. The example code is shown below.<\/p>\n<pre>\n#include\n#include\n\nvoid print_env(char * env_var){\n\n  char *path = getenv (env_var);\n\n  if (path!=NULL){\n    printf (\" %s \\n\",path);\n  }\n  else{\n    printf(\" %s not found in the environment\\n\", env_var);\n  }\n}\nint main ()\n{\n  print_env(\"HOME\");\n  putenv(\"C_ENV_SET=C set the env\");\n  print_env(\"C_ENV_SET\");\n  return 0;\n}\n\n<\/pre>\n<\/li>\n<li>\n<p>Java provides <em><strong>System.getenv()<\/strong><\/em> method to read the environment variable but it doesn\u2019t provide method to set the environment variable. Java asserts this kind of operation as insecure and hence JVM doesn\u2019t 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 \u00a0consider System.setProperty() and Sytem.getProperty() as an option. The following code shows how to read the system variables.<\/p>\n<pre>\nimport java.util.Map;\n\npublic class EnvRead {\n    public static void main (String[] args) {\n        Map env = System.getenv();\n         System.out.println(env.get(\"HOME\"));\n\n    }\n}\n<\/pre>\n<\/li>\n<li>\n<p>It\u2019s really straight forward to read and set the environment variable in Shell script. It uses echo and export command.<\/p>\n<pre>\n#!\/bin\/sh\n\n#Read the environment variable.\necho $HOME\n\n#Set the environment variable\nexport SH_ENV_SET='export set the env'\n\n#read the Set environment variable.\necho $SH_ENV_SET\n<\/pre>\n<\/li>\n<li>\n<p>Python provides OS module which has <em><strong>environ<\/strong><\/em> mapping object representing the string environment. The following code is to access and set the environment variable.<\/p>\n<pre>\n#!\/usr\/bin\/env python\n\nimport os\n\n#For accessing the environment variable.\nprint os.environ['HOME']\n\n#For setting the environment variable.\nos.environ['PY_ENV_SET']='environ set the env'\n\nprint os.environ['PY_ENV_SET']\n<\/pre>\n<\/li>\n<li>\n<p>Sometimes even in build script we need to access environment variables. Accessing environment variable is easy and it is shown \u00a0below. Gradle is similar to java and runs in JVM and hence it also has similar\u00a0restrictions\u00a0as that of Java for setting environment variable.<\/p>\n<pre>\ntask env_read<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>How to read environment variable in 5 different languages? 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. In C, environment variables can be read and set by getenv() and putevn() methods respectively. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/8","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":0,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"wp:attachment":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}