macos - 为什么我无法在 macOS 上更改环境变量 $USERNAME?

标签 macos environment-variables zsh iterm2 direnv

我想做什么

$ echo $USERNAME
myusername
$ export USERNAME=newvalue
$ echo $USERNAME
newvalue

发生了什么
$ echo $USERNAME
myusername
$ export USERNAME=newvalue
$ echo $USERNAME
myusername

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

  • 有用的笔记
  • 操作系统:macOS Mojave 10.14
  • 目录:https://github.com/direnv/direnv
  • 我正在使用 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/

    相关文章:

    macos - 安装了触发器应用程序,安装了node.js,触发器说它无法在Mac上运行npm

    macos - 找不到 .zshrc 文件

    bash - 如何在远程服务器上为 git commit、git pull、git push 和 pull 创建 bash 或 zsh 别名?

    cron - 如何从 Cron 作业访问 Docker 集环境变量

    json - 将 bash 变量传递给 jq

    apache - RewriteBase 的值可用作变量/引用吗?

    time - 为什么 `time` 在 `echo` 上不起作用?

    ruby - 在 XCode 中编码/运行 Ruby

    macos - 如何模拟慢速DVD驱动器?

    javascript - 带有隐藏滚动条的 Lion/Mountain Lion 上的 CSS 悬停错误