0

I have 2 Macs, an old iMac in my study and a MacMini in my workshop. I have iTerm2 on both of them. The MacMini is new and I'm still fine tuning the setup. I use zsh for my shell on both and the same .zshenv on both. I have done all I can to make sure I'm using the same iTerm2 settings on both. The problem is that I can set the prompt in .zshenv on my iMac, but the same .zshenv script does not do a thing to the prompt on my new MacMini. (And I have changed the shell to zsh and have checked to be sure I have.) I am NOT using Oh-my-zsh. I just found out about that and may add it later, but this issue is really irritating me and I want to get it fixed.

Here's the relevant code:

PROMPT='%F{magenta}%B[%b%D %* %n@%m %1~%B]%b $ %f'
uname=`whoami`
if [[ "$uname" == "root" ]]; then
    PROMPT='%F{red}%B[%b%D %* %n@%m %1~%B]%b $ %f'
fi
echo "Prompt = $PROMPT"
export LS_COLORS
echo "About to export prompt"
echo "Prompt = $PROMPT"
export PROMPT

I'm getting the following behaviors:

  • When I open a new window or tab, it reports the proper value for $PROMPT from the script. It does not change my prompt from %n@%m %1~ %#.
  • When I check the value (after .zshenv as run), it comes out to %n@%m %1~ %#.
  • If I copy and paste the line from the script that sets the prompt, it changes the prompt to what I want.
  • If I type ./.zshenv it runs and I get DOUBLE output from the echo commands, as if it's run the script twice and it does not change the prompt.
  • If I type source .zshenv it runs, I don't get repeated output, and the prompt changes.
  • If I copy the line that defines the prompt from the file and paste it into the terminal, it changes the prompt.
  • When I check other variable values and aliases from .zshenv, they have been imported into the shell from the script.

I also just checked on the normal terminal program. I had not thought to check this before. I get the same behaviors on the normal Mac terminal program, too.

This is the same script from my older Mac (which as been kept up to date with regular Apple upgrades), but it behaves differently on this Mac.

1
  • Considering your description (and ignoring the meaningless attempts such as ./.zshenv, I would conclude that PROMPT (or the equivalent PS1) gets overwritten after you set it. Of course ~/.zshenv is an unusual location for setting the prompt (we ususally would do it in ~/.zshrc), but you can easily check where the prompt gets overwritten by starting a new shell with zsh -lx. Aug 22, 2022 at 15:19

1 Answer 1

1

The issue is that zsh also reads files from /etc as well as your own ones.

Apple now provides a /etc/zshrc that includes

# Default prompt
PS1="%n@%m %1~ %# "

thus removing your changes. (In Mojave - ie before zsh was made the default /etc/zshrc does not contain this )

The zsh manual gives the startup files order.

Commands are first read from /etc/zshenv; this cannot be overridden. Subsequent behaviour is modified by the RCS and GLOBAL_RCS options; the former affects all startup files, while the second only affects global startup files (those shown here with an path starting with a /). If one of the options is unset at any point, any subsequent startup file(s) of the corresponding type will not be read. It is also possible for a file in $ZDOTDIR to re-enable GLOBAL_RCS. Both RCS and GLOBAL_RCS are set by default.

Commands are then read from $ZDOTDIR/.zshenv. If the shell is a login shell, commands are read from /etc/zprofile and then $ZDOTDIR/.zprofile. Then, if the shell is interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc. Finally, if the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlogin are read.

The manuals say that zshenv files are read by all zsh invocations and zshrc is read for interactive. As running in the terminal and showing the prompt is only used when you want to type in the terminal then $PS1 should only be set in zshrc files and not zshenv files .

That is move your code from ~/.zshenv to ~/.zshrc

2
  • When I got the notice that Apple was moving to zsh, I decided, since I was between projects, to make the changes on all my systems to move to zsh over bash. Somewhere, and I can't remember where, I saw some references to using .zshenv. That source had NO reference to .zshrc. I changed the name of the file to .zshrc and it worked fine. Also, somehow, without me ever remember doing it, on my older Mac, I had .zshrc. Have no idea what happened there, since I just can't remember making that file by copying or editing or anything else. Thanks!
    – Tango
    Aug 22, 2022 at 18:12
  • 1
    Use Sturgeon's law 90% of articles are crap. In this case zsh has very good documentation use that rather than random blogs.
    – mmmmmm
    Aug 22, 2022 at 18:16

You must log in to answer this question.

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