4

WHAT I AM USING

  • zsh
  • MacOS Mojave 14.10

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

Tried to set USERNAME in .bashrc, .zshrc, .profile and nothing changes...

Tried to unset and reset and nothing changes...

  • This variable is readonly and you can change it by switching user – Romeo Ninov Nov 22 '18 at 15:26
  • The weird thing is I am quite sure that I was able to change it, then I had probably messed up with something.. – Riccardo Persiani Nov 22 '18 at 15:27
  • Do you see readonly USERNAME or declare -r USERNAME anywhere? Either of those would mark a variable as read-only. – telcoM Nov 22 '18 at 15:29
  • 1
    This is not a variable that is set by default on macOS. Are you setting it yourself somewhere? – Kusalananda Nov 22 '18 at 15:38
  • 2
    what does typeset -p USERNAME say? – mosvy Nov 22 '18 at 15:38
12

The USERNAME shell variable is special in the Z shell.

It is always the account name of the user ID of the shell process.

You won't observe the behaviour that you observed in the Almquist, Watanabe, Korn, or Bourne Again shells. This variable is just an ordinary shell variable, that starts out unset, as far as they are concerned. Here's the 93 Korn shell, for example:

$ echo $USERNAME

$ USERNAME=wibble
$ echo $USERNAME
wibble
$

In the Z shell it starts out as the account name of the UID of the shell process. An attempt to set it will attempt to change that UID. This of course fails if you are not the superuser and leaves the variable back as it was before.

If you had run the Z shell as the superuser, however, you would have seen both the USERNAME shell variable and your shell process's user ID change.

root # echo $USERNAME
root
root # USERNAME=JdeBP
JdeBP %

Further reading

  • Running as a superuser I am able to change the variable. Thank you – Riccardo Persiani Nov 22 '18 at 15:54
  • 2
    @RiccardoPersiani Ummm... switching to root is not a "solution". Why do you need to change the value of this variable? – Kusalananda Nov 22 '18 at 16:10
  • I agree. However, I also can change it, simply switching from zsh to bash. – Riccardo Persiani Nov 22 '18 at 16:34
  • 2
    Note that there can be more than one user name for a user id, zsh reports the one returned by getpwuid() so an arbitrary one (one could argue it should use logname() instead). Changing $USERNAME does a setuid(), but also setgid()/setgroups() with the groups mentioned in the user database as when you log in as that user name, that's different from setting $UID or $EUID for instance. So by doing a USERNAME=$USERNAME you might very well change the list of groups of the process if there are several usernames for the current uid with different group membership. – Stéphane Chazelas Nov 22 '18 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.