MacOS – Synchronise or share .zshrc between accounts

command linemacosmojavezsh

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?

Best Answer

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.