How to Delete exported environment Variable in Linux shell

Export variables in the Linux shell are a special kind of environmental variables. This kind of variable is used to export the values from the parent shell to the child shell. Also, it can be used in the subshells and new sessions of the terminals. 

This blog will explain the methods to delete the exported environment variables in the Linux shell. Let’s start with the method of displaying the “Exported Variables” in the Linux shell. 

How to Find the Exported Environment Variable in Linux Shell?

Use the grep command to find the “exported variables” in the Linux shell. For instance, run the following command to display all the export variables in Linux:

$ env

We can see that the “MY_VARIABLE” is the variable we exported in the Linux shell. 

What are the Methods to Delete Exported Environment Variables in Linux Shell?

Two different approaches can be used to delete the exported environment variable in the Linux shell:

  1. Using the unset Command
  2. Using the env Command

Method 1: Delete the Exported Environment Variable Using the unset Command

The first and recommended method to delete the exported environment variable is by using the unset command. 

Following the steps, we will delete the “MY_VARIABLE” in the Linux shell with the unset command.

Step 1: Open the Terminal

First, use the CTRL+ALT+T to launch the terminal of Linux:

Step 2: Display the Export Variable

Display the export variable with its value using the grep command:

$ env | grep MY_VARIABLE

The stored value in the “MY_VARIABLE” has been displayed on the screen. 

Step 3: Delete the Export Variable

Now use the unset command to delete the “MY_VARIABLE” in the Linux shell:

$ unset MY_VARIABLE

Step 4: Verify the Deletion of Exported Environment Variable

Finally, repeat the step 3 for the confirmation of the deletion of the exported environment variable with the unset command:

$ env | grep MY_VARIABLE

The exported environment variable has successfully been deleted from the Linux shell by using the unset command. 

Method 2: Delete the Exported Environment Variable Using the env Command

Another method is to delete the value of the exported environment variable in the Linux shell, after which no value will be exported to the next shells. To understand this, create the exported variable with the “ILF” name:

$ export ILF="Hello"

Confirm the creation of the exported environment variable with the grep command:

$ env | grep ILF

Now to delete the value of the “ILF” variable, run the command:

$ env -i bash

The above command has opened the new bash shell by deleting the exported variables in the last shell created by the user. To verify this, display the exported environment variable with the command:

$ env | grep ILF

The exported environment variable has not been exported to new shells. 

These are the two different methods for deleting the exported environment variable in the Linux shell. 

Conclusion

To delete the exported environment variable in the Linux shell, use the unset command. To use the unset command, open the terminal and run “unset [variable name]”. Another approach to deleting the export variable is to open the new shell with the “i” option as it will clear the exported variables. In this blog, both deletion methods of exported environment variables have been explained in detail.