Why am I no more able to change the environment variable $USERNAME on macOS?

1.7k Views Asked by At

WHAT I WANT TO DO

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

WHAT IS HAPPENING

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

WHAT I TRIED

  • I tried to use: sudo ...;
  • I tried to use: unset USERNAME.

USEFUL NOTES

WHAT I DID BEFORE THE ISSUE

I was able to change my environment variable several times using direnv (https://github.com/direnv/direnv), and everything was working well.

I was able to set local env variables in .envrc. Then, I encountered this issue...


SOLUTION

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

1

There are 1 best solutions below

0
On

In zsh the USERNAME var is magical. It is not a normal exported env var. From the man page:

   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)'

In other shells, like bash and fish, this is not a special var and you can set it just like any other env var:

bash$ echo $USERNAME

bash$ export USERNAME=wtf
bash$ echo $USERNAME
wtf