sramij
sramij

Reputation: 4925

Customize zsh prompt for normal/superuser?

How can I make zsh shell use "$ and #" for "normal and superuser" instead of "% and #"?

Upvotes: 2

Views: 2201

Answers (3)

sramij
sramij

Reputation: 4925

Adding the following to ~/.zshrc did the trick for me:

PROMPT='%(!.#.$) '

It puts # for root, and $ for others.

Upvotes: 2

Bach Lien
Bach Lien

Reputation: 1060

Check EUID (effective user id) in ~/.zshrc:

if [[ $EUID == 0 ]]; then
    PROMPT='# '
else
    PROMPT='$ '
fi

Upvotes: 0

chepner
chepner

Reputation: 532103

The currently deleted self-answer is correct, so far as it goes. In a prompt, %(!.#.$) will expand to # if the effective user ID of the shell is 0, $ otherwise.

However, be aware that if your current prompt is something like PS1='%(!.#.$) ', simply starting a privileged shell may not preserve the current value of PS1, even if exported. For example, on macOS, the default /etc/zshrc file explicitly sets the value of PS1, discarding any value you may have expected to be "inherited".

That said, I'd recommend against using $ in the prompt, as it is heavily associated with Bourne-shell descendants that don't deviate quite so much from standard interactive behavior. zsh provides a large number of features designed explicitly for (and conflicting with standard) interactive use, so a % prompt provides a good reminder of that.

Upvotes: 0

Related Questions