#!/bin/bash # This file is mainly used for bash shell prompt (PS1) configuration. # Reference material for further reading - http://www.gnu.org/software/bash/manual/bashref.html#Controlling-the-Prompt # Depends: https://github.com/lhunath/scripts/blob/master/bashlib/bashlib # Non-UTF8 prompt. Use this when term does not support UTF8. #PS1="\n\A \[$green\]\[$bold\]\u\[$reset\]@\[$blue\]\H\[$reset\]:\l: \[$bold\]\[$cyan\]\w\[$reset\] [\$?]\[$bold\]\$(__git_ps1)\[$reset\] \n\$ " # PROMPT_COMMAND runs before the prompt starts. # Here I specify the colors of the exit status indicator, based on the exit status. More info - http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Exit_Status # 0 gets green, 1 gets red and everything else gets a mellow yellow. PROMPT_COMMAND='case $? in 0) exit_color=$green ;; 1) exit_color=$red ;; *) exit_color=$yellow ;; esac' # Here I specify an array of colors used to give a non-0 job indicator a color. # So that means that when there are some jobs in the background, the job indicator will get colored. job_colors=( [0]="$bold$yellow" ) # PS1 depending if the user has root privileges or not. # Do `sudo bash` and then see the prompt change. (This however might not work on many Red Hat based systems, primarily because of SELinux isolation) if (( UID > 0 )); then PS1='\n┌───| \[$bold$cyan\]\w\[$reset\] ](\[$bold$green\]\u\[$reset\]@\[$blue\]\H\[$reset\])[$SHELL]|[\[$white\]\A\[$reset\]][bn:\l]\[${job_colors[\j==0]}\][j:\j]\[$reset\]► [\[$exit_color\]$?\[$reset\]]\[$bold\]$(__git_ps1)\[$reset\] \n└─▪ '; else PS1='\n┌───| \[$bold$red\]\w\[$reset\] ](\[$bold$red\]\u\[$reset\]@\[$blue\]\H\[$reset\])[$SHELL]|[\[$white\]\A\[$reset\]][bn:\l]\[${job_colors[\j==0]}\][j:\j]\[$reset\]► [\[$exit_color\]$?\[$reset\]]\[$bold\]$(__git_ps1)\[$reset\] \n└─▪ '; fi # Also note the \[${job_colors[\j==0]}\][j:\j]\[$reset\] part. # Let's break this down a bit. # You have the ${job_colors[\j==0]} array operation encased inside the non-printing character sequence indicators ( the things that start with \[ and end with \] ) # You can do arithmetic tests within bash arrays. In our case, this means that when the amount of background jobs equals 0 (\j==0), the outcome of the test will be equal to 'true'. # When outcome of the test is equal to true, it will choose the first item in the array (in this case the $bold$yellow we have specified in the job_colors array. # To not color any further than we need, we reset the color by issuing \[$reset\].