if [ "$SHLVL" -le 1 ]; then
_ps1_code="0;7"
else
_ps1_code="$(( ($SHLVL - 2) / 6 % 2));3$((6 - ($SHLVL - 2) % 6))"
fi
export PS1='\[\e[36m\]\@ \[\e[30;1m\]\h:\w \[\e['"$_ps1_code"'m\]\$\[\e[0m\] '
unset _ps1_code
12:17 PM foreigner:/etc $ bash
12:17 PM foreigner:~ $ bash
12:18 PM foreigner:~ $ bash
12:18 PM foreigner:~ $ _
This uses the value of
$SHLVL to color the '$' in the prompt. A login shell will display with reverse video; deeper shells (e.g. when using
:sh in vim) will get a color according to their depth. In this case, it goes 'backwards' from cyan to red, then bold cyan to bold red, before repeating the colors. Due to the special handling of level 1, reverse video is never repeated in the cycle. Also, since the SHLVL is supposed to be constant in a given shell, the prompt color is calculated once, and the result is inserted into PS1.
The deeper purpose of this code is to give me some sort of visual indication of whether I'm in the base level shell (reverse video) or a deeper one (color). The difference in colors allows me to notice if I unintentionally ran vim again from inside another shell, instead of exiting the shell to return to vim.
The preview is displayed in the
BlackRock theme.