How to Delete an Exported Environment Variable in Shell in Linux
A variable is the basic building block that stores a value in a specific memory location, and any operation performed on the variable affects that memory location. Values stored in variables can be changed or deleted during program execution. Environment variables are the variables whose value is set global in the program and can be inherited by the child shell. In this article, we will see the different methods that can be used to remove exported environment variables in the shell.
Local vs user vs system wide environment variables
- Local environment variables: These variables are specific to the current session and could be lost when it ends.
- User environment variables: User configuration files define user environment variables. When a user logs in, they are always loaded from the home directory.
- System wide variables: Variables that apply to the entire system rather than just one session or user are known as system wide variables. They may, for instance, be universal variables that apply to all users.
Creating Variable With Export Command
Using the Export command, we will first create an Environment variable.
export message="Hello from GeeksforGeeks"
Using the env command, we will check the Environment variable.
env | grep message

There are many ways to clear the exported environment variables in the shell, some of which are explained as follows:
Method 1: Clear the value of an Exported Variable by assigning an empty value.
Note: Using this method, we cannot delete the variable from the environment, but we can delete the value of the exported variable by assigning a null value.
export message=
Now using the env command we will check the message.
env | grep message
You can see that we have assigned the empty value to the message variable so we have got the empty message as an output.

Method 2: Delete an Exported Environment Variable in Shell Using Unset Command
The Unset command is an in-built shell command that can be used to clear the value of an exported variable. Let’s use this command to remove an exported environment variable and verify that the exported variable was removed:
unset message
env | grep message
By using the unset command we have deleted the message variable.

Method 3: Clear the value of an Exported Variable with the env Command
With the help of the below env command we can clear the value of an exported variable:
env -i bash
In the above command, we have -i which will clear user-exported variables and start a new Bash session with default values.

To check that variable is present in the older bash session run the following command:
exit
env | grep message

Please Login to comment...