我想做什么
$ echo $USERNAME
myusername
$ export USERNAME=newvalue
$ echo $USERNAME
newvalue
发生了什么
$ echo $USERNAME
myusername
$ export USERNAME=newvalue
$ echo $USERNAME
myusername
我试过的
sudo
...; unset USERNAME
. 有用的笔记
zsh
我在发布之前做了什么
我可以使用
direnv
多次更改我的环境变量( https://github.com/direnv/direnv ),一切都运行良好。我能够在
.envrc
中设置本地环境变量.然后,我遇到了这个问题......
解决方案
https://unix.stackexchange.com/questions/483469/cannot-change-the-environment-variable
最佳答案
在 zsh 中 USERNAME
var 是神奇的。它不是正常的导出环境变量。从手册页:
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)'
在其他 shell 中,例如 bash 和 fish,这不是一个特殊的 var,您可以像设置任何其他 env var 一样设置它:
bash$ echo $USERNAME
bash$ export USERNAME=wtf
bash$ echo $USERNAME
wtf
关于macos - 为什么我无法在 macOS 上更改环境变量 $USERNAME?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53418303/