Delete expoted environment Variable in Linux shell


Overview

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 −

$ env | grep -i 'EXPORTED_VAR'

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.

Updated on: 23-Dec-2022

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements