How do I delete an exported environment variable in Linux?
To delete an exported environment variable in Linux, you can use the built-in unset command. This removes the variable from the current shell’s environment, so it won’t be recognized afterward. For example, if your variable is named MY_VAR:
unset MY_VAR
Once unset, any child processes spawned from the current shell session will also not have this variable. If you need to remove an environment variable permanently, ensure you delete or comment out its export line in your shell’s configuration files (e.g., ~/.bashrc, ~/.zshrc, or ~/.bash_profile) and then reload your shell or start a new session.
Why unset Works
- Ephemeral Environment: When you
exporta variable, it is only set for the current session and its child processes. - Built-In Command:
unsetis a shell built-in, meaning it directly modifies the shell’s environment. - No Need for
export -n: Some older or less common shells might mention variations likeexport -n, but in Bash and similar shells,unsetis the standard approach.
Example Workflow
-
Check if a variable is set:
echo $MY_VARIf you see something like
MyValue, then it’s still active. -
Unset the variable:
unset MY_VAR -
Verify removal:
echo $MY_VARYou should see a blank line (or no output), indicating the variable is no longer defined.
-
Remove from config (if permanent removal is desired):
- Open your
~/.bashrcor~/.zshrc(or the file where you originallyexported the variable). - Delete or comment out the line containing
export MY_VAR=.... - Reload your shell:
or start a new terminal session.source ~/.bashrc
- Open your
Further Learning
To improve your overall command-line and problem-solving abilities, check out the following courses from DesignGurus.io:
-
Grokking Data Structures & Algorithms for Coding Interviews
Master essential data structures and algorithms that will make your scripting and development tasks more efficient. -
Grokking the Coding Interview: Patterns for Coding Questions
Learn the core patterns repeatedly tested in technical interviews, giving you a strong foundation for problem-solving in any language.
By understanding environment variables and mastering coding fundamentals, you’ll be well-prepared to tackle both everyday administration tasks and complex software challenges.