pastebin - collaborative debugging tool
bash.kpaste.net RSS


Untitled
Posted by Anonymous on Sun 25th Oct 2015 18:04
raw | new post

  1. #!/bin/bash
  2. # This file is mainly used for bash shell prompt (PS1) configuration.
  3. # Reference material for further reading - http://www.gnu.org/software/bash/manual/bashref.html#Controlling-the-Prompt
  4.  
  5. # Depends: https://github.com/lhunath/scripts/blob/master/bashlib/bashlib
  6.  
  7. # Non-UTF8 prompt. Use this when term does not support UTF8.
  8. #PS1="\n\A \[$green\]\[$bold\]\u\[$reset\]@\[$blue\]\H\[$reset\]:\l: \[$bold\]\[$cyan\]\w\[$reset\] [\$?]\[$bold\]\$(__git_ps1)\[$reset\] \n\$ "
  9.  
  10. # PROMPT_COMMAND runs before the prompt starts.
  11. # 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
  12. # 0 gets green, 1 gets red and everything else gets a mellow yellow.
  13. PROMPT_COMMAND='case $? in
  14. 0) exit_color=$green ;;
  15. 1) exit_color=$red ;;
  16. *) exit_color=$yellow ;;
  17. esac'
  18.  
  19. # Here I specify an array of colors used to give a non-0 job indicator a color.
  20. # So that means that when there are some jobs in the background, the job indicator will get colored.
  21. job_colors=( [0]="$bold$yellow" )
  22.  
  23. # PS1 depending if the user has root privileges or not.
  24. # 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)
  25. if (( UID > 0 ));
  26.  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└─▪ ';
  27.   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└─▪ ';
  28. fi
  29.  
  30. # Also note the \[${job_colors[\j==0]}\][j:\j]\[$reset\] part.
  31. # Let's break this down a bit.
  32. # 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 \] )
  33. # 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'.
  34. # 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.
  35. # To not color any further than we need, we reset the color by issuing \[$reset\].

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at