为什么我不再能够在macOS上更改环境变量$ USERNAME?

Ric*_*ani 3 macos zsh environment-variables iterm2 direnv

我想做的事

$ echo $USERNAME
myusername
$ export USERNAME=newvalue
$ echo $USERNAME
newvalue
Run Code Online (Sandbox Code Playgroud)

怎么了

$ echo $USERNAME
myusername
$ export USERNAME=newvalue
$ echo $USERNAME
myusername
Run Code Online (Sandbox Code Playgroud)

我尝试过的

  • 我尝试使用:sudo...;
  • 我尝试使用:unset USERNAME

有用的笔记

发布之前我做了什么

我能够使用direnvhttps://github.com/direnv/direnv)多次更改环境变量,并且一切运行良好。

我能够在中设置本地环境变量.envrc。然后,我遇到了这个问题...


https://unix.stackexchange.com/questions/483469/cannot-change-the-environment-variable

Kur*_*der 5

在zsh中,USERNAMEvar是神奇的。这不是正常的导出环境变量。从手册页:

   USERNAME <S>
          The username corresponding to the real user ID of the shell process.  If you
          have sufficient privileges, you may change the username (and also  the  user
          ID  and group ID) of the shell by assigning to this parameter.  Also (assum-
          ing sufficient privileges), you may start a single command under a different
          username (and user ID and group ID) by `(USERNAME=username; command)'
Run Code Online (Sandbox Code Playgroud)

在bash和fish这样的其他shell中,这不是一个特殊的var,您可以像设置任何其他env var一样进行设置:

bash$ echo $USERNAME

bash$ export USERNAME=wtf
bash$ echo $USERNAME
wtf
Run Code Online (Sandbox Code Playgroud)