Just a Rum-Soaked Space Hobo with a Heart full of Love
Hey, check out my neat bash prompt:
GIT_PS1_SHOWCOLORHINTS='y'
GIT_PS1_SHOWDIRTYSTATE='y'
GIT_PS1_SHOWSTASHSTATE='y'
GIT_PS1_SHOWUNTRACKEDFILES='y'
GIT_PS1_DESCRIBE_STYLE='contains'
GIT_PS1_SHOWUPSTREAM='auto'
_noop[0]='array accesses turn 0/nonzero into defined/undef!'
declare -A _session
_session["tty"]='array accesses turn tty/* into defined/undef!'
PS1='\[\e[$(($?==0?0:91));$(($?==0?1:5))m\]\$\[\e[0m\] ' # $?-coloured $
PS1='$(__git_ps1 "\[\e[1;33m\]{%s\[\e[1;33m\]}")'$PS1 # Gold git
PS1='\[\e[1;34m\]\w'$PS1 # Blue CWD
PS1='${SSH_CONNECTION:+\[\e[1;32m\]\h\[\e[1;33m\]:}'$PS1 # Green hostname, gold :
PS1='${_session[$XDG_SESSION_TYPE]:+\[\e[1;35m\]\u${SUDO_USER:+\[\e[1;91m\]($SUDO_USER)}\[\e[1;33m\]@}'$PS1 # Purple username
PS1='${debian_chroot:+\[\e[1;36m\]($debian_chroot)}'$PS1 # Cyan chroot
PS1='${_noop[$((\j==0))]:+\[\e[1;33m\][\j]}'$PS1 # gold nonzero jobs
PS1='${_noop[$(($?==0))]:+\[\e[1;91m\]($?)}'$PS1 # Red nonzero $?
The prompt environment is fairly limited. It can do parameter expansion, reference expansion, arithmetic expansion, and a couple other neat tricks. You can shell out, but if you do that overwrites the variable that contains the exit code of the last program you ran ($?). Tools such as the __git_ps1 hacks do their best to only use features of bash that work internally to bash itself.
This prompt uses the _noop array (with only ${_noop[0]} defined) to turn arithmetic expressions into defined/undefined responses. This lets us use some of the brace-expansion features to specify alternate output, so we can display the error code or number of jobs in the background only if they're non-zero, and we can style them with a high degree of freedom.
It does a similar trick with a string-indexed associative array, and only displays the hostname if the $XDG_SESSION_TYPE is "tty" (which happens in ssh or sudo sessions). It dynamically displays the $SUDO_USER if that is available.
What this means is that on your local desktop system, when you log in, your prompt is merely:
~$
But the same prompt on a remote system will show the more common:
user@hostname:~$
And if you are in a chroot with background processes looking at a git repo after a command that exited with an error code of 1:
(1)[2](thechroot)user@hostname:~/src/therepo{main $%}$
with coloured styling information on all the various parts.
I just think it's neat!