I wanted to create a short, informational and colourful prompt.My prompt includes:Exit status of last command (if not 0)Distinctive changes when rootrsync-style user@host:pathname for copy-paste goodnessGit branch, index, modified, untracked and upstream informationPretty coloursExample: Is there a way to achieve a similar result with less lines of code?In ~/.bashrc:## Set the prompt ### Select git info displayed, see /usr/lib/git-core/git-sh-prompt for moreexport GIT_PS1_SHOWDIRTYSTATE=1           # '*'=unstaged, '+'=stagedexport GIT_PS1_SHOWSTASHSTATE=1           # '$'=stashedexport GIT_PS1_SHOWUNTRACKEDFILES=1       # '%'=untrackedexport GIT_PS1_SHOWUPSTREAM=verbose     # 'u='=no difference, 'u+1'=ahead by 1 commitexport GIT_PS1_STATESEPARATOR=''          # No space between branch and index statusexport GIT_PS1_DESCRIBE_STYLE=describe  # detached HEAD style:#  contains      relative to newer annotated tag (v1.6.3.2~35)#  branch        relative to newer tag or branch (master~4)#  describe      relative to older annotated tag (v1.6.3.1-13-gdd42c2f)#  default       exactly eatching tag# Check if we support colours__colour_enabled() {    local -i colors=$(tput colors 2>/dev/null)    [[ $? -eq 0 ]] && [[ $colors -gt 2 ]]}unset __colourise_prompt && __colour_enabled && __colourise_prompt=1__set_bash_prompt(){    local exit=$? # Save the exit status of the last command    # PS1 is made from $PreGitPS1 + <git-status> + $PostGitPS1    local PreGitPS1=${debian_chroot:+($debian_chroot)}    local PostGitPS1=    if [[ $__colourise_prompt ]]; then        export GIT_PS1_SHOWCOLORHINTS=1        # Wrap the colour codes between \[ and \], so that        # bash counts the correct number of characters for line wrapping:        local Red='\[\e[0;31m\]'; local BRed='\[\e[1;31m\]'        local Gre='\[\e[0;32m\]'; local BGre='\[\e[1;32m\]'        local Yel='\[\e[0;33m\]'; local BYel='\[\e[1;33m\]'        local Blu='\[\e[0;34m\]'; local BBlu='\[\e[1;34m\]'        local Mag='\[\e[0;35m\]'; local BMag='\[\e[1;35m\]'        local Cya='\[\e[0;36m\]'; local BCya='\[\e[1;36m\]'        local Whi='\[\e[0;37m\]'; local BWhi='\[\e[1;37m\]'        local None='\[\e[0m\]' # Return to default colour        # No username and bright colour if root        if [[ ${EUID} == 0 ]]; then            PreGitPS1+=$BRed\h         else            PreGitPS1+=$Red\u@\h$None:        fi        PreGitPS1+=$Blu\w$None    else # No colour        # Sets prompt like: ravi@boxy:~/prj/sample_app        unset GIT_PS1_SHOWCOLORHINTS        PreGitPS1=${debian_chroot:+($debian_chroot)}\u@\h:\w    fi    # Now build the part after git's status    # Highlight non-standard exit codes    if [[ $exit != 0 ]]; then        PostGitPS1=$Red[$exit]    fi    # Change colour of prompt if root    if [[ ${EUID} == 0 ]]; then        PostGitPS1+=$BRed'\$ '$None    else        PostGitPS1+=$Mag'\$ '$None    fi    # Set PS1 from $PreGitPS1 + <git-status> + $PostGitPS1    __git_ps1 $PreGitPS1 $PostGitPS1 '(%s)'    # echo '$PS1='$PS1 # debug        # defaut Linux Mint 17.2 user prompt:    # PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[00m\] $(__git_ps1 (%s)) \$ '}# This tells bash to reinterpret PS1 after every command, which we# need because __git_ps1 will return different text and colorsPROMPT_COMMAND=__set_bash_prompt[Note: the current code based upon feedback received is here]
Coloured bash prompt including git status and previous exit code
bash;console;git
Solid script, nicely done.less lines of code__colour_enabled is used only once,when setting __colourise_prompt, which in turn is also used only once.It seems an unnecessary indirection.You could drop this line:unset __colourise_prompt && __colour_enabled && __colourise_prompt=1And change this condition:if [[ $__colourise_prompt ]]; thenTo this:if __colour_enabled; thenSome color variables are defined but never used.But I would keep them anyway,in case you (or somebody else) want to adjust the colors later(they are not exactly easy to remember).To shorten the code, you could rewrite some of the if statements in cmd && ... || ... form, but I don't recommend that.In its current form it's nicely readable and good that way.Undefined variablesWhen you detect that colors are enabled, you set a bunch of variables, for example Red, BRed, Mag, None. If colors are not enabled, you don't clear these variables, but they may be used anyway later in the script. Although it may seem unlikely that these variables are set outside this script, it can happen,and that will be confusing and possibly annoying.The clean solution is to make sure to set those variables to blank when colors are not enabled.Condition without pattern matchingThe == operator in [[ is useful for pattern matching.In this example you don't need pattern matching,so I suggest to replace it with =, which will do simple literal comparison.if [[ ${EUID} == 0 ]]; thenCoding styleYou mixed two styles of function declaration:__colour_enabled() {    # ...}__set_bash_prompt(){    # ...}It would be better to use consistent style (I prefer the first one).