Open In App

How to Delete an Exported Environment Variable in Shell in Linux

Last Updated : 06 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 globally 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

exportedvaribaleandverifythevaribale-651ea36de4a17

Export Command

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.

assigningemptyvalue-651ea36f888b0

Assigning empty value

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.

unset-651ea36eebbb2

Unset Command

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.

2-651ea36ec3444

env Command

To check that variable is present in the older bash session run the following command:

exit
env | grep message

3-651ea36e1baa2

Checking Variable

Conclusion

In summary, environment variables are fundamental for storing global values in a shell. We explored methods to manage them: assigning an empty value using ‘export‘, removing them with ‘unset’, and resetting with ‘env’. These techniques are vital for efficient shell scripting and system configuration, granting control over processes and applications in Unix-like environments. Mastering these methods empowers effective manipulation of environment variables for diverse needs in shell programming and system administration.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads