how to view, edit, unset and delete an environment variable in linux

On Linux and/or Unix operating systems, an environment or system variable is a name-value pair that is available system wide and is used to share information between applications. These (pre) named objects are usually used as configuration settings that an application can lookup provided it knows the name of the variable or object.

In addition to the system-wide variables, a variable can also be set into other scopes such as an user specific and session specific variables. It can also be set and unset from a single instance of the shell or session.

Also, we will mostly deal with the bash command shell in this post. But it will work in other shells as well probably with slight changes in the syntax. The basic concept of environment variables remain the same across shells.

Display a Single Environment Variable

If you know the name of the variable that you want to lookup, then you can use the echo command to display the value of the variable. This works with in shell scripts as well.

bash$ echo $<variable name>

The $ is used to denote that the string following it is a variable. Usually, the environment variables are all upper-case, but it need not be. It is mostly a case of convention. If the variable name is HOME, then the command is

bash$ echo $HOME

Another command that can be used to display a variable value is printenv. This works pretty much the same way as the echo command works in the previous example.

bash$ printenv HOME

Note that the special character $ is not required by this command, as arguments to this command  are assumed to be environment variable names.

Screenshot of printenv command in konsole

Display all variables

In case you want to see all the system variables that are set in the environment, then you can use any of the three commands : set, env or printenv. Each of these commands when executed without any command line arguments, will display all the variables and their values that are currently available in the shell environment.

bash$ set
bash$ env
bash$ printenv

The output of the above commands can get pretty lengthy. As you already know or will find out, there are quite a long list of environment properties set by default. This also means that it can get quite difficult to find a specific key or property value.

If you do only know part of a variable name or you want to display only a subset of the variables, then you can pipe the output of previous command to grep. The example below will display all variables that have the word proxy in the name or the value.

bash$ env | grep -i proxy

This section showed you how you can view the environment variables on the system. Often time you would want to set or modify the values as well. You can do this directly from the command shell as well.

Set an Environment Variable

Setting an environment variable is the same as creating a new value or editing an existing value. When you set a variable, the variable is created and appended to the environment if it does not already exist. If it already exist in the environment, then the value is modified to reflect the changes.

You can use either the set or env command to set the variable using the following syntax. Both the commands should work in most command shells.These commands will set the environment variable for the current shell or instance.

If you find that one of the commands is not supported by your particular shell, then just use the other one. Also, the syntax could be different. Some shells do not require the equals to (=) symbol in the command either.

bash$ set NAME=Value
bash$ env NAME=Value

Because of how variables are inherited by processes in Linux and depending on how and where it is executed, the variables may have been set only on the current session or the current instance of the shell. In order to set it globally, you can use either the export or the setenv command depending on the command shell that is used.

In bourne or bash shells, use the export command as in the example below.

bash$ export NAME=Value

while in csh and related shells, use the setenv command

csh$ setenv NAME Value

(Note: There is no equals (=) operator in setenv).

Do not forget to include the previous value of a variable, if you only want to append to the existing value. Otherwise, the previous value of the variable is overwritten. This is relevant in path variables like PATH or LIBRARY_PATH. To append a directory, called /home/john/temp into the existing path, use something like the following

bash$ export PATH=$PATH:/home/john/temp

Again exporting the value will only set it for the current session. That means when you logout or restart the system, the changes will be lost. If you want to persist your value changes across reboots, then you will need to modify the configuration file(s) where it is set.

Global Environment Values

The global environment values are set in various places, often varying between distros. If it is a user specific variable, then it is quite possible that it is set in the shell specific configuration file in the user’s home directory.

If you are using the bash shell, then check out the .bashrc or .bash_profile files in the home directory. Some environments also have a .profile file in the home directory. Another file that is used as default for all users and shells is the /etc/profile file.

In a Gentoo environment, you can set global environment values in a file inside the /etc/env.d/ folder. This allows you to have separate files for different sets of the global environment variables. There are also several different profile files in the /etc/ folder, such profile, profile.csh, profile.env etc etc. Also, there are configuration files inside the /etc/profile.d/ folder.

If you use Ubuntu or an Ubuntu based distro, then the global variables are set in the /etc/environment file. You can also look inside the /etc/profile.d/ folder, which can several files including .sh file that are executed. The same format is used in other distros as well such CentOS. In Suse based systems, also look for a file called /etc/profile.local.

Configuration files for global env variables in linux

Yep, there is a lot of different configuration files where the global variables could be set. See my set of configuration files in the screenshot above. With great Power comes lots of configuration files. Usually in practice, it is not that bad. Everybody has individual preferences and the variables will go into similar files and folders and once you know and understand how it works, you are all set.

Once you have found the place or file where the environment variable is currently defined or set, you can edit the file to change the value and save it. Follow the existing formats in the files when modifying the values. They are usually the same syntax you use when setting the variables from the command line. The changes will now be persisted across logouts and reboots.

If this is a new variable that you want to add to the system, then you will need to find the most appropriate place out of all the options mentioned above. If this new variable is local and applicable only to some users, then set it in the home directory. If it is to be accessible only to some users in the command shell, then set it in the appropriate shell configuration files. If it is a truly global then set it in the global configuration files in the /etc/ folders.

Delete (or Unset) an Environment Variable

Sometimes you want to completely remove the variable from the environment. In order to remove or unset a variable from the environment, you can again use the env command with the –unset (-u) command line option.

bash$ env -u NAME

Another command which does the same thing is the unset command. The unset is a posix command and should be available in all shells, but you never know.

bash$ unset NAME

After un-setting the variable, be sure to check it using either the echo or set commands to make sure that it has indeed been removed.

As with the set commands earlier, if you want to persist the change to the value then you will need to modify and save the configuration file where it is set. As you are removing or unsetting the values in this case, just remove the line where the value is set.