{"id":10970,"date":"2021-10-15T10:39:46","date_gmt":"2021-10-15T10:39:46","guid":{"rendered":"https:\/\/linuxways.net\/?p=10970"},"modified":"2021-10-15T10:39:46","modified_gmt":"2021-10-15T10:39:46","slug":"how-to-set-unset-environment-variables-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-set-unset-environment-variables-in-linux\/","title":{"rendered":"How to Set\/Unset Environment Variables in Linux"},"content":{"rendered":"<p>Environment variables are a type of variables that are defined in the shell and are required during program execution. They are often used in shell programs to perform various operations.<\/p>\n<p>The scope of any variable refers to the area in which it can be accessed or defined. In Linux, the scope of an environment variable can either be global \/system-wide or local. Some common examples of environment variables in Linux include:<\/p>\n<ul>\n<li>PWD &#8211; displays the path of the present working directory<\/li>\n<li>HOME &#8211; gives the path of the home directory<\/li>\n<li>HOSTNAME &#8211; prints name of the host<\/li>\n<li>EDITOR &#8211; shows the default file editor<\/li>\n<\/ul>\n<p>In this guide, we will learn how to set and unset environment variables in Linux.<\/p>\n<h2><strong>Set environment variables using the export command<\/strong><\/h2>\n<p>A simple way to set environment variables is using the export command. We have to give the variable a name that will be used to access it in the shell and a value to hold data<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre><strong>$ export NAME = VALUE<\/strong><\/pre>\n<p>For example, set you variable name<\/p>\n<pre><strong>$ export VAR =\"value\"<\/strong><\/pre>\n<p>When using the export command, the environment variable will be set only for the present shell session. As a result, the environment variable will no longer be accessible if you open a new terminal or restart your system.<\/p>\n<p>To output the value of your environment variable on the shell, you can use either of the commands shown;<\/p>\n<pre><strong>$ printenv variable<\/strong><\/pre>\n<pre><strong>$ echo $variable<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"151\" class=\"wp-image-10971\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-137.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-137.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-137-300x62.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/strong><\/p>\n<p>Note: When using the echo command, the variable name should be preceded by a dollar sign.<\/p>\n<h2><strong>Set user-wide environment variables on Linux<\/strong><\/h2>\n<p>These are the variables that have been defined for a specific user and are executed whenever that user logs in via a local or remote login session. They are set in and loaded from the following configuration files in the user&#8217;s home directory: <strong>bashrc, .bash profile, .bash login, <\/strong>and<strong> .profile.<\/strong><\/p>\n<p><strong>Using the .bashrc file<\/strong><\/p>\n<p>The .<strong>bashrc<\/strong> file is a script that is loaded whenever a user opens a new terminal session. Environment variables in this file are executed whenever a new session is started.<\/p>\n<p>For example, add a variable NEW_VAR to your .bashrc file as shown:<\/p>\n<pre><strong>$ sudo vi .bashrc<\/strong><\/pre>\n<pre><strong>$ export NEW_VAR =\u201dLinux\u201d<\/strong><\/pre>\n<p>Save your file and reload the .bashrc file with the following source command for changes to be applied:<\/p>\n<pre><strong>$ source ~\/.bashrc<\/strong><\/pre>\n<p>Print out the new variable:<\/p>\n<pre><strong>$ echo $NEW_VAR<\/strong><\/pre>\n<p>The variable is now persistent when you open new sessions or restart your system<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"835\" height=\"182\" class=\"wp-image-10972\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-138.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-138.png 835w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-138-300x65.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-138-768x167.png 768w\" sizes=\"auto, (max-width: 835px) 100vw, 835px\" \/><\/p>\n<p><strong>Using .bash_profile<\/strong><\/p>\n<h3>To add environment variables that will be available for remote login sessions., modify the .bash_profile file.<\/h3>\n<pre><strong>$ sudo vi .bash_profile<\/strong><\/pre>\n<pre><strong>$ export $VAR1 =\"Linux\"<\/strong><\/pre>\n<p>Next, reload the .bashrc file with the following source command for changes to take effect.<\/p>\n<pre><strong>$ source ~\/.bash_profile<\/strong><\/pre>\n<p>Print out the new variable:<\/p>\n<pre><strong>$ echo $VAR1<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"855\" height=\"286\" class=\"wp-image-10973\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-139.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-139.png 855w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-139-300x100.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-139-768x257.png 768w\" sizes=\"auto, (max-width: 855px) 100vw, 855px\" \/><\/p>\n<p><strong>Set system-wide environment variables in Linux<\/strong><\/p>\n<p>These are system-wide environment variables, meaning they are available to all users on the system. These variables can be found in the following directories and files that include system-wide configuration files: \/etc\/environment, \/etc\/profile, \/etc\/profile.d\/, \/etc\/bash.bashrc, \/etc\/profile.d\/, \/etc\/profile.d\/, \/etc\/profile.d\/, \/etc\/profile.d\/, \/etc\/profile<\/p>\n<p><strong>Using \/etc\/bash.bashrc file<\/strong><\/p>\n<p>Add a system-wide environment variable to the<strong> \/etc\/bash.bashrc<\/strong> file. It will be available to the users when any of them starts a new terminal but cannot be accessed remotely.<\/p>\n<pre><strong>$ export VAR1='Linux'<\/strong><\/pre>\n<p>Now, reload the \/etc\/bash.bashrc file for changes to take effect.<\/p>\n<pre><strong>$ source \/etc\/bash.bashrc <\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"880\" height=\"200\" class=\"wp-image-10974\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-140.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-140.png 880w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-140-300x68.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-140-768x175.png 768w\" sizes=\"auto, (max-width: 880px) 100vw, 880px\" \/><\/strong><\/p>\n<p>From the output above, the variable can be accessed by both the normal user and root.<\/p>\n<p><strong>Using \/etc\/profile file<\/strong><\/p>\n<p>To add an environment variable that will be available when any of the users on your system is accessing it remotely, modify the <strong>&#8216;\/etc\/profile&#8217;<\/strong> file. After adding the variable, reload the file.<\/p>\n<pre><strong>$ sudo vi \/etc\/profile<\/strong><\/pre>\n<pre><strong>$ export VAR='Linux'<\/strong><\/pre>\n<pre><strong>$ source \/etc\/profile<\/strong><\/pre>\n<pre><strong>$ echo $VAR<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"871\" height=\"560\" class=\"wp-image-10975\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-141.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-141.png 871w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-141-300x193.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-141-768x494.png 768w\" sizes=\"auto, (max-width: 871px) 100vw, 871px\" \/><\/strong><\/p>\n<p>From the example above, the variable is available for a new user who has logged in remotely<\/p>\n<p><strong>Using \/etc\/environment file<\/strong><\/p>\n<p>To add an environment variable that will be available to all users on both local and remote login sessions, add the variable in the <strong>\/etc\/environment<\/strong> file.<\/p>\n<pre><strong>$ sudo vi \/etc\/environment<\/strong><\/pre>\n<pre><strong>$ export MY_VAR='Linux'<\/strong><\/pre>\n<pre><strong>$ source \/etc\/environment<\/strong><\/pre>\n<pre><strong>$ echo $MY_VAR<\/strong><\/pre>\n<h2><strong>Unset Environment Variables on Linux<\/strong><\/h2>\n<p>Now that you have learnt how to set environment variables on your system, you may need to remove them if you don&#8217;t need them anymore.<\/p>\n<p>To unset environment variables in Linux use the unset command or remove variable entries from your system configuration files.<\/p>\n<p>Use the following syntax to unset an environment variable in Linux:<\/p>\n<pre><strong>$ unset &lt;variable&gt;<\/strong><\/pre>\n<p>For example, unset any of the above variables:<\/p>\n<pre><strong>$ unset NEW_VAR<\/strong><\/pre>\n<p>To verify this, print out the variable:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"873\" height=\"149\" class=\"wp-image-10976\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-142.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-142.png 873w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-142-300x51.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-142-768x131.png 768w\" sizes=\"auto, (max-width: 873px) 100vw, 873px\" \/><\/p>\n<p>Nothing is displayed in the terminal.<\/p>\n<p><strong>Note<\/strong>: Environment variables defined in the system-wide or user-wide configuration files can be erased solely by removing them from these files. After that, reload the file with the source command for changes to take effect.<\/p>\n<pre><strong>$ source &lt;file-name&gt;<\/strong><\/pre>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>You have seen how to set and unset both local and persistent environment variables in Linux.<\/p>","protected":false},"excerpt":{"rendered":"<p>Environment variables are a type of variables that are defined in the shell and are required during program execution. They are often used in shell programs to perform&hellip;<\/p>","protected":false},"author":1,"featured_media":11189,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[35,607],"class_list":["post-10970","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-linux","tag-variables"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/10970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=10970"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/10970\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/11189"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=10970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=10970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=10970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}