Sorry for the necro-bump.. but I have revisited this thread after a couple of years using the following in my .shrc
and wondered If I can fix one minor annoyance.
cd() {
command cd -- "$@"
S="$(dirname "$PWD")/$(basename "$PWD")"
R="$(dirname "$HOME")/$(basename "$HOME")"
S=$(echo "$S" | tr -s /) # /// -> /
R=$(echo "$R" | tr -s /) # /// -> /
if [[ $(id -u) -eq 0 ]]
then P='\033[31m'#''
else P='λ'
fi
if [[ "$S/" == "$R/"* ]] # ksh(1), bash(1) ver.
then PS1=$(printf '\033[90m%s\033[0m \033[93m'$P'\033[0m ' "~${S#$R}")
else PS1=$(printf '\033[90m%s\033[0m \033[93m'$P'\033[0m ' "$S")
fi
}
cd
This is excellent, however, I wish that this prompts was achievable without needing to use cd
so that when I su
to root I can remain in the CWD.
For example:
~ λ pwd
/home/dave
~ λ cd Downloads/
~/Downloads λ su
Password:
~ # pwd
/root
preferably I'd su
and remain in /user/dave/Downloads
Here is my bash
PS1 that works to great effect:
user_color=$'\e[94;1m'
prompt_char=%
if [[ $(id -u) -eq 0 ]] ; then
user_color=$'\e[31;1m'
prompt_char='#'
fi
PS1='\[$user_color\][\u]\[\e[0m\] \[\e[90;1m\]\w\[\e[0m\] \[$user_color\]$prompt_char\[\e[0m\] '
Is what I'm after possible @rvp ?