1

I have a standard user account (in Mojave using zsh shell) and based on Gentoo Wiki I have added this to my .zshrc to show a prompt like [user@computer] ~ $

if [[ $UID == 0 ]]; then
   PS1="[%n@%M] %~ # "
else
   PS1="[%n@%M] %~ $ "
fi

I changed root shell to zsh with sudo chsh -s /bin/zsh but sudo su gives an incorrect (presumably default) prompt unless I re-source my .zshrc:

[hali@A1398] ~ $ sudo su
A1398# echo $PS1
%m%#
A1398# source .zshrc
[root@A1398] /Users/hali #

Based on sourcing bash profile for root users and What are the practical differences between Bash and Zsh I copied my .zshrc for root to use with cp ~/.zshrc /var/root/

Similarly I also have a hidden admin account set up according to this Apple doc and so su admin also shows its' default prompt unless I also do cp ~/.zshrc /var/admin/

If I copy both it then works as I want - the user(%n) is shown before the computer name(%M):

[hali@A1398] ~ $ sudo cp .zshrc /var/root/
[hali@A1398] ~ $ sudo cp .zshrc /var/admin/
[hali@A1398] ~ $ sudo su
[root@A1398] /Users/hali # su admin
[admin@A1398] /Users/hali $ exit
[root@A1398] /Users/hali # exit
[hali@A1398] ~ $

Is there any way to use one copy of .zshrc for these three accounts to share or somehow automatically keep them in sync as I make further changes to this file?

2
  • Does sudo su -l fix the issue,in other words, prevent the need to source .zshrc ?
    – fd0
    Commented Feb 19, 2020 at 19:33
  • @fd0 - unfortunately sudo su -l didn't work so I make symlinks for .zshrc and also ran touch /var/root/.histfile as zsh-newuser-install didn't seem to work for root user.
    – lx07
    Commented Feb 20, 2020 at 8:20

2 Answers 2

2

Use a symlink. It uses the ln command in the terminal

ln -s ~/.zshrc /var/root/.zshrc

You can replace /var/root with whatever you need to. This will create a file named .zsrhc in /var/root that is linked to the original copy in your home directory. Please note that you may need sudo before this if you do not have file permissions.

1

Zsh will read up to 5 "global" rc files on startup depending on its shell classification, ie. login or non-login, interactive or non-interactive and their content is available to all zsh users. These files are /etc/zshenv, /etc/zprofile, /etc/zshrc, /etc/zlogin, and /etc/zlogout. They are read in conjunction with 5 "dot files", usually found in your home directory .zshenv, .zprofile, .zshrc, .zshlogin, and .zshlogout. In your case, /etc/zshrc would be a good candidate.

You can replace-

if [[ $UID == 0 ]]; then
   PS1="[%n@%M] %~ # "
else
   PS1="[%n@%M] %~ $ "
fi

with this one line

PROMPT='[%n@%M] %~ %(!.#.$) '

The %(!.#.$) is a conditional statement which reads:

if the shell is privileged (!) then place the text # at the end of the prompt else place a $ at the end of the prompt. (This syntax seems alot like cshsyntax.)

Enjoy!

1
  • Thank you - this answer works very well too. I assumed before trying it that /etc would be protected in Catalina but it seems not. I just need to decide whether to make changes global or not.
    – lx07
    Commented Feb 21, 2020 at 18:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .