Delete expoted environment Variable in Linux shell

This article explains how to delete an exported environment variable from the Linux shell. Environment variables are key-value pairs that store system and user configuration information accessible to all processes running in the system.

Understanding Environment Variables

An environment variable is a named storage location that contains data used by the operating system and applications. When a variable is exported, it becomes available to child processes spawned from the current shell. These variables are stored in the process's memory space and can be accessed programmatically using functions like getenv() in C.

Listing Exported Variables

Before deleting environment variables, you can view all currently exported variables using the env command:

$ env

To search for a specific variable, use grep to filter the output:

$ env | grep VARIABLE_NAME

Method 1: Using the unset Command

The unset command is the primary method to completely remove an environment variable from the shell session. This command removes both the variable and its value from the environment.

$ export file_name="test.txt"
$ env | grep file_name
file_name=test.txt
$ unset file_name
$ env | grep file_name

After running unset, the variable is completely removed from the environment, and any attempt to access it will return nothing.

Method 2: Assigning Empty Value

You can clear an environment variable's value by assigning it an empty string. However, this method does not remove the variable itself?it only empties its content:

$ export file_name="test.txt"
$ env | grep file_name
file_name=test.txt
$ export file_name=""
$ env | grep file_name
file_name=

The variable still exists in the environment but contains no value. This approach is useful when you want to keep the variable defined but temporarily clear its content.

Method 3: Using env Command with Clean Environment

The env command with the -i flag creates a clean environment by removing all existing environment variables and starting a new shell session:

$ export file_name="test.txt"
$ env | grep file_name
file_name=test.txt
$ env -i bash
$ env | grep file_name

This method starts a new bash session with minimal default environment variables. When you exit this clean session, the original environment variables are restored:

$ exit
$ env | grep file_name
file_name=test.txt

Comparison of Methods

Method Command Effect Use Case
Unset unset VAR Removes variable completely Permanent removal from session
Empty Assignment export VAR="" Clears value, keeps variable Temporary value clearing
Clean Environment env -i bash Creates minimal environment Testing with clean state

Conclusion

Deleting exported environment variables in Linux can be accomplished using unset for complete removal, empty assignment for value clearing, or env -i for clean environment testing. The unset command is the most common and effective method for permanently removing variables from the current shell session.

Updated on: 2026-03-17T09:01:38+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements