Featured image of post Why does a deleted .zshrc environment variable still take effect? Why doesn't source ~/.zshrc clear old variables? Why does reopening terminal windows keep environment variables? How to completely clear environment variables in IDE built-in terminals and Terminal apps?

Why does a deleted .zshrc environment variable still take effect? Why doesn't source ~/.zshrc clear old variables? Why does reopening terminal windows keep environment variables? How to completely clear environment variables in IDE built-in terminals and Terminal apps?

When environment variables deleted from .zshrc remain active, it is often due to residual memory or parent process inheritance. Learn how to clear persistent environment variables using unset, restarting IDE/terminal apps, and checking launchctl.

Even though you have already deleted an environment variable (such as ANTHROPIC_BASE_URL) from ~/.zshrc, when you check the terminal, it is surprisingly still there!

Even re-running source ~/.zshrc is completely useless?

How to Check If an Environment Variable Exists and Which Config File Wrote It?

Before cleaning up, you first need to confirm whether the variable still remains in current memory, and which configuration file is automatically loading it.

1. Check Memory Variables

Check if the environment variable still remains in the current Terminal memory

env | grep -i anthropic

2. Search Configuration File Sources

Comprehensively search Zsh, Bash, and system-level configuration files to find which file wrote the variable

grep -rn "ANTHROPIC_BASE_URL" ~/.zshrc ~/.zprofile ~/.zshenv ~/.zlogin ~/.bashrc ~/.bash_profile ~/.profile /etc/zshenv /etc/zprofile /etc/profile /etc/environment 2>/dev/null

Why Do Old Environment Variables Remain After Modifying .zshrc?

Actually, when you open a terminal, environment variables are already loaded into the “memory” of that connection.

Even if you modify the file or re-execute source ~/.zshrc, this only “reloads” the content in the config file and does not automatically purge old variables existing in memory.

The source command only overwrites or adds variables, and will not “delete” variables for you

At this point, the simplest solutions are as follows:

Cleanup Method Command / Action Description
Open New Window Open a brand new terminal tab The new Shell Process will re-read the latest ~/.zshrc
Manual Clearance unset ANTHROPIC_BASE_URL Directly delete the specified environment variable from current memory
Reset Shell exec zsh -l Reload the current Shell memory state

Reopening Terminal Windows Still Useless? Uncover the Top 3 Culprits

Why is it that sometimes even after opening a brand new terminal window, old environment variables still linger around?

In this case, the problem is no longer in ~/.zshrc, but inherits from the “Parent Process”!

When you launch a terminal from a software application, the child process directly copies the parent process environment variable snapshot.

If you encounter such persistent environment variables, it is usually due to the following three common reasons:

Type Cause Solution
IDE Built-in Terminal VS Code, Cursor, or Antigravity recorded an environment variable snapshot upon startup Press Cmd + Q to completely quit IDE and reopen it
Terminal Software Not Closed Terminal or iTerm2 on Mac only closed windows (Cmd + W), main application runs in background Press Cmd + Q to completely quit Terminal software
tmux tmux background Server runs continuously, new tabs keep inheriting settings from launch Run tmux kill-server to stop background service

Reopening Software Still Ineffective? Check Third-Party Tools and System-Level Settings

If all three tricks above fail and environment variables stubbornly appear in Terminal, higher-level system settings or third-party tools may be working behind the scenes.

Higher system permissions or wrapper scripts will restore environment variables automatically upon each launch

In this case, you can use the following two main detection commands:

Detection Target Command Description
System Global Variables launchctl getenv ANTHROPIC_BASE_URL Check if third-party proxy tools use launchctl setenv to set global variables
Command Wrapper Scripts type claude Check if current command is original executable or wrapped in a wrapper script with default variables

Standard Troubleshooting Steps for Completely Clearing Environment Variables

Next time you modify or delete environment variables in ~/.zshrc, remember to first confirm: Does current terminal inherit old Parent Process settings?

Follow this troubleshooting sequence to easily find the root cause and resolve the issue:

  1. First execute unset VARIABLE_NAME or open a brand new terminal window.
  2. If using IDE like VS Code or Cursor, press Cmd + Q to completely restart IDE.
  3. If using tmux, run tmux kill-server to reset background service.
  4. Check launchctl getenv and type commands to troubleshoot system levels and wrapper scripts.
All rights reserved,未經允許不得隨意轉載
Built with Hugo
Theme Stack designed by Jimmy