Last Updated: May 02, 2019
·
3.157K
· vsviridov

Sudo-aware shell prompt

LOGNAME=$(logname)
UUSER=$([ "${LOGNAME}" = "${USER}" ] && echo ${USER} || echo '$(tput setaf 1)${LOGNAME}$(tput sgr0) as ${USER}')
PS1="[${UUSER}@\h \W]\\$ "

Find a section of your global bashrc, responsible for setting the prompt and modify the PS1 part, replacing \u with ${UUSER}.

Next time you sudo - it will tell you that you are impersonating.

Picture

3 Responses
Add your response

This seems like it would be a great oh-my-zsh plugin.

over 1 year ago ·

I'm not a zsh user, but feel free :)

over 1 year ago ·

Or you could do this better with $PROMPT_COMMAND
https://gitorious.org/demure/dotfiles/source/subbash/prompt

over 1 year ago ·