{"id":3302,"date":"2025-02-19T20:41:16","date_gmt":"2025-02-19T20:41:16","guid":{"rendered":"https:\/\/foghosting.com\/how-to-set-environment-variables-in-linux\/"},"modified":"2025-02-19T20:41:16","modified_gmt":"2025-02-19T20:41:16","slug":"how-to-set-environment-variables-in-linux","status":"publish","type":"post","link":"https:\/\/foghosting.com\/blog\/how-to-set-environment-variables-in-linux\/","title":{"rendered":"How to Set Environment Variables in Linux?"},"content":{"rendered":"<div id=\"content\">\n<div role=\"main\">\n<div>\n<figure><\/figure>\n<\/div>\n<div>\n<h2><span id=\"Introduction\">Introduction<\/span><\/h2>\n<p>An environment variable is like a magic wand that can give information on the location of specific directories, passwords, and API keys. This system variable has vast possibilities in terms of software and application development. Do you know this variable can be configured differently on each system? Yes, every operating system has its own set of variables that can be configured as per the requirement.\u00a0<\/p>\n<p>This article will focus mainly on how to <strong>set environment variables in Linux<\/strong>. Moreover, you will get a wide-angle view of the most common examples relating to this exceptional setting. Let\u2019s start by reading about the basic concept of this variable.\u00a0<\/p>\n<h2><span id=\"Understanding_Environment_Variables\">Understanding Environment Variables<\/span><\/h2>\n<figure><\/figure>\n<p>Environment variables make application deployment easier in different environments. Also, it is easy to change the environment variable runtime, which leads to dynamic applications and programs. This system variable offers the following functionalities:\u00a0<\/p>\n<ol>\n<li><strong>Configuration<\/strong>: Helps customize application behavior.<\/li>\n<\/ol>\n<ol start=\"2\">\n<li><strong>Security<\/strong>: Responsibly stores sensitive information with high-end security.<\/li>\n<\/ol>\n<ol start=\"3\">\n<li><strong>Inter-process communication<\/strong>: Let the programs easily share data between processes.<\/li>\n<\/ol>\n<ol start=\"4\">\n<li><strong>Platform Independence<\/strong>: Gives support for portable applications across systems.<\/li>\n<\/ol>\n<ol start=\"5\">\n<li><strong>Dynamic Behavior<\/strong>: Allows for runtime changes to pave the way for dynamic application.<\/li>\n<\/ol>\n<ol start=\"6\">\n<li><strong>Ease of Deployment<\/strong>: Environment variables lead to simple deployment of applications.<\/li>\n<\/ol>\n<p>Each operating system has its own set of variables, and you can also <strong>set environment variables in Linux<\/strong>. Having the right system settings or commands will help you place the proper variable. Now, let\u2019s move on to some common examples of these specified variables in Linux.\u00a0<\/p>\n<h2><span id=\"Common_Examples_of_Environment_Variables_in_Linux\">Common Examples of Environment Variables in Linux<\/span><\/h2>\n<figure><\/figure>\n<p>There are different ways to use <strong>environment variables in Linux<\/strong>, and we have listed the most common ones for your convenience. Go through this quick list of examples.<\/p>\n<ol>\n<li><strong>PATH<\/strong>: Every computer has an executable program that can be run with just a simple click. The \u201cPATH\u201d variable shows the location of these executable programs.\u00a0<\/li>\n<\/ol>\n<ol start=\"2\">\n<li><strong>HOME<\/strong>: Many files and configurations are stored in the home directory. The way to this directory is easily accessible through the \u201cHOME\u201d variable.<\/li>\n<\/ol>\n<ol start=\"3\">\n<li><strong>USER<\/strong>: This helps in storing the credentials of the current user about the system.\u00a0<\/li>\n<\/ol>\n<ol start=\"4\">\n<li><strong>LANG<\/strong>: If you need to find the default, then simply use the \u201cLANG\u201d variable.<\/li>\n<\/ol>\n<ol start=\"5\">\n<li><strong>DISPLAY<\/strong>: Everything on the computer screen is controlled through the server manager. A \u201cDISPLAY\u201d variable specifies this server manager for the client.\u00a0<\/li>\n<\/ol>\n<ol start=\"6\">\n<li><strong>EDITOR<\/strong>: Linux uses a command line interface that works with a text editor. The \u201cEDITOR\u201d variable specifies the default text editor to be used.\u00a0<\/li>\n<\/ol>\n<ol start=\"7\">\n<li><strong>TZ<\/strong>: This variable helps set the timezone for programs.\u00a0<\/li>\n<\/ol>\n<h2><span id=\"How_to_Check_the_Environment_Variable\">How to Check the Environment Variable?<\/span><\/h2>\n<p>Checking the environment variable in Linux is helpful during the setting up process. The user gets the details of the instructions attached to the variable during the checking process. If there is an anomaly or requirement for a change in the program behavior, then the user must check the environment variable. Let\u2019s take a look at how to get through this process.<\/p>\n<ul>\n<li><strong>Using the echo command<\/strong>: Use the following command to print the value of the \u201cPATH\u201d:<\/li>\n<\/ul>\n<figure><\/figure>\n<ul>\n<li>Using the printenv command: You can print all the environment variables using the command below.<\/li>\n<\/ul>\n<figure><\/figure>\n<figure><\/figure>\n<p>If there is any need to print a specific variable, then simply use the following command and specify the name of the variable in the syntax:<\/p>\n<figure><\/figure>\n<figure><\/figure>\n<p>There are two methods for setting environment variables in Linux: temporary and permanent. The content below will look at each category in detail.\u00a0<\/p>\n<h3><span id=\"How_to_set_environment_variables_in_Linux_Temporarily\"><strong>How to set environment variables in Linux Temporarily?<\/strong><\/span><\/h3>\n<p>The duration between a particular log-in and log-out is called a session. As a user, you can set specific variables to set these sessions and avoid the permanent setting. The \u201cecho\u201d command will be used to perform the function. Take a look at the syntax below.<\/p>\n<figure><\/figure>\n<p>Here, we have set the value of \u201cMY_VARIABLE\u201d as \u201cexample.\u201d The term \u201cvalue\u201d refers to the content of the variable.\u00a0<\/p>\n<h3><span id=\"Steps_to_set_permanent_environment_variables_in_Linux\"><strong>Steps to set permanent environment variables in Linux\u00a0<\/strong><\/span><\/h3>\n<p>There are two different approaches in <strong>Linux to set environment variables permanently<\/strong>. Let\u2019s start with the very first method.<\/p>\n<ul>\n<li><strong>\u00a0~\/.bash_profile<\/strong>:<\/li>\n<\/ul>\n<p>Simply add the variable to the \u201c~\/.bash_profile\u201d file in the home directory. Use the following command to do so:\u00a0<\/p>\n<figure><\/figure>\n<p>Save the file once the command is placed.\u00a0<\/p>\n<ul>\n<li><strong>~\/.profile<\/strong>:<\/li>\n<\/ul>\n<p>If the default shell is not Bash, then simply add the variable in the \u201c~\/.profile\u201d file. Use the command below to open the \u201c~\/.profile\u201d file through the text editor.<\/p>\n<figure><\/figure>\n<p>Now use the following command to add the variable to this file.<\/p>\n<figure><\/figure>\n<p>Replace the term \u201cMY_VARIABLE\u201d with the chosen variable name and the term \u201cexample\u201d with the required value. At last, save the changes and exit using the \u201cCtrl + O\u201d and \u201cCtrl + X\u201d systematically.\u00a0<\/p>\n<p>Also Read: DNS Cache Flushing: Troubleshooting Guide for All Operating Systems<\/p>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>The setup of environment variables plays a crucial role in Linux systems. It is important for flexibility and customization for various applications and system settings. The variables serve as dynamic tools that facilitate application deployment, configuration, security, and inter-process communication. Understanding <strong>how to set environment variables in Linux<\/strong>, whether temporarily or permanently, empowers users to tailor their system environment to suit specific needs.\u00a0<\/p>\n<p>You can easily edit the variable on a permanent or temporary basis using methods like \u201c~\/.bash_profile\u201d or \u201c~\/.profile\u201d. The user can easily enhance the efficiency and functionality of their Linux systems. Whether it\u2019s configuring common variables like PATH or HOME, or defining custom variables for specific applications, the setup of environment variables in Linux is essential for effective system administration and development.<\/p>\n<\/p><\/div>\n<div>\n<div>\n<h3>Arpit Saini<\/h3>\n<p>\n\t\t\t\t\tHe is the Director of Cloud Operations at Fog Hosting and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.\t\t\t\t<\/p>\n<\/p><\/div><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction An environment variable is like a magic wand that can give information on the location of specific directories, passwords, and API keys. This system variable has vast possibilities in terms of software and application development. Do you know this variable can be configured differently on each system? Yes, every operating system has its own<\/p>\n","protected":false},"author":3,"featured_media":3076,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-3302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dedicated-servers"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts\/3302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/comments?post=3302"}],"version-history":[{"count":0,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts\/3302\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/media\/3076"}],"wp:attachment":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/media?parent=3302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/categories?post=3302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/tags?post=3302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}