Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  • OS: macOS Mojave 10.14

  • Direnv: https://github.com/direnv/direnv

  • I am using zsh

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

like image 676
Riccardo Persiani Avatar asked Nov 21 '18 18:11

Riccardo Persiani


1 Answers

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
like image 90
Kurtis Rader Avatar answered Nov 05 '22 04:11

Kurtis Rader