How do I delete an exported environment variable in Linux?

To delete an exported environment variable in Linux, you need to understand how environment variables work in shells. Here’s a detailed guide with examples:

1. Understanding Environment Variables

  • Exported variables are available to the current shell and its child processes (e.g., scripts or commands launched from the shell).
  • Non-exported variables are local to the current shell only.
  • Variables become “exported” when you use export VARIABLE_NAME or set them inline (e.g., export VAR=value).

2. Delete an Exported Variable

Use the unset command to remove the variable from the current shell session and its child processes.

Command Syntax:

unset VARIABLE_NAME

Example:

# Step 1: Export a variable
export MY_VAR="Hello World"

# Step 2: Verify it exists
echo $MY_VAR          # Output: Hello World
env | grep MY_VAR     # Output: MY_VAR=Hello World

# Step 3: Delete the variable
unset MY_VAR

# Step 4: Confirm deletion
echo $MY_VAR          # Output: (empty)
env | grep MY_VAR     # Output: (no result)

3. Remove Persistence from Startup Files

If the variable was added to shell startup files (e.g., ~/.bashrc, ~/.bash_profile, /etc/environment), it will reappear in new shell sessions. To permanently delete it:

Step 1: Edit the Startup File

# Example: Remove from ~/.bashrc
nano ~/.bashrc

Step 2: Delete or Comment Out the Line

Find the line containing export VARIABLE_NAME=value and either:

  • Delete it.
  • Comment it out with #:
  # export MY_VAR="Hello World"  # Disabled

Step 3: Apply Changes

Either:

  • Reload the file in the current shell:
  source ~/.bashrc
  • Restart the shell.

4. Remove the “Exported” Status Only

If you want to keep the variable as a shell variable (non-exported) but remove its exported status, use:

export -n VARIABLE_NAME

Example:

export MY_VAR="Hello World"
export -n MY_VAR     # MY_VAR is now a shell variable (not exported)
env | grep MY_VAR    # Output: (empty)
echo $MY_VAR         # Output: Hello World (still exists in the shell)

5. More Examples

Example 1: Delete a Variable with Special Characters

export SPECIAL_VAR="value_with_!@#"
unset SPECIAL_VAR

Example 2: Delete Variables in Scripts

#!/bin/bash
export TEMP_VAR="Temporary"
echo "Before unset: $TEMP_VAR"  # Output: Temporary
unset TEMP_VAR
echo "After unset: $TEMP_VAR"   # Output: (empty)

Example 3: Delete Multiple Variables

export VAR1="A" VAR2="B"
unset VAR1 VAR2    # Delete both variables

Example 4: Remove Variables Set in Parent Shells

If a variable was exported in a parent shell (e.g., a terminal session), you can’t delete it from a child shell (e.g., a script). You must unset it in the parent shell directly.

6. Notes

  • Read-Only Variables: If a variable is marked as read-only (readonly VAR), you cannot unset it until the shell session ends.
  • Check All Exports: Use env or printenv to list all exported variables.
  • Special Variables: Some variables (e.g., PATH, HOME) are critical for system operation. Avoid unsetting them unless necessary.

Summary

  • unset: Deletes the variable entirely.
  • export -n: Removes the exported status but keeps the variable in the shell.
  • Startup Files: Edit files like ~/.bashrc to prevent variables from reappearing.

By following these steps, you can manage environment variables effectively in Linux!

Leave a Reply

Your email address will not be published. Required fields are marked *