- how to view, edit, unset and delete an environment variable in linux
- Display a Single Environment Variable
- Display all variables
- Set an Environment Variable
- Global Environment Values
- Delete (or Unset) an Environment Variable
- Delete expoted environment Variable in Linux shell
- Environment Variable
- List of exported Variables in Linux shell
- Using the unset Command
- Assigning the Empty Value
- Using the env Command
- Conclusion
This post and this website contains affiliate links. See my disclosure about affiliate links.
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.
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
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.
Note that the special character $ is not required by this command, as arguments to this command are assumed to be environment variable names.
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.
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
Or
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.
while in csh and related shells, use the setenv command
(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.
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.
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.
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.
Delete expoted environment Variable in Linux shell
This article explains how to delete an environment variable from the system.
An environment variable is a special kind of variable that can be used by all processes running on your machine. The name of this variable is EXPORTED_VARIABLE. You can use it to pass information between different programs or even between different users. For example, you might want to export the path to the directory where you store your music files so that other users can access them easily.
Environment Variable
The environment variables are stored as part of the process’s memory space and they are accessible through the getenv() function. It returns a pointer to the value of the specified environment variable. If there is no such variable, then NULL will be returned.
List of exported Variables in Linux shell
You can list out all the currently exported environment variables using the env command. This command lists all the environment variables which have been set for the current user.
$ env | grep -i '^EXPORT' EXPORTPATH/usr/bin:/usr/local/bin:/home/user1/bin
You can see the list of exported variables using the command −
Using the unset Command
The unset command is a shell command that clears the value of an exported environment varaible. This means that if you run unset EXPORTED_VARAIBLE, then any process will no longer have access to the value of the variable. If you want to remove the variable completely, you need to run unset -f EXPORTED_VARIABLE.
Let’s use this command and see if we get any results.
$ export file_name="test.txt" $ env | grep file_name file_name=test.txt $ unset file_name $ env | grep file_name
We’re verifying that the variable has been removed from our shell environment by running the env command.
Assigning the Empty Value
You can clear the values of exported variables by assigning them empty values.
$ export file_name="test.txt" $ env | grep file_name file_name=test.txt $ export file_name= $ env | grep file_name file_name=
It doesn’t actually remove the variables from the environment; rather, it simply sets them to an empty string.
Using the env Command
The env command lets us run programs in a modified environment. We can then use this command to reset the export status of an exported variable.
$ export file_name="test.txt" $ env | grep file_name file_name=test.txt $ env -i bash $ env | grep file_name
We’re using the −i flag here, which clears the variables that were previously set by the user and starts another bash shell with the defaults.
Exit from the new bash session and check if the environment variables are present in the older one.
$ exit $ env | grep file_name file_name=test.txt
Conclusion
We looked at several different ways to export an environment variable so that you can use it later. We can use these command line tools to make our shell script code more robust.