pastebin - collaborative debugging tool
bash.kpaste.net RSS


PS1_improved
Posted by Anonymous on Mon 26th Oct 2015 10:36
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. # Thanks to all the knowledgeable people in #bash @ irc.freenode.net
  5.  
  6. # Depends: https://github.com/lhunath/scripts/blob/master/bashlib/bashlib
  7.  
  8. # Non-UTF8 prompt. Use this when term does not support UTF8.
  9. #PS1="\n\A \[$green\]\[$bold\]\u\[$reset\]@\[$blue\]\H\[$reset\]:\l: \[$bold\]\[$cyan\]\w\[$reset\] [\$?]\[$bold\]\$(__git_ps1)\[$reset\] \n\$ "
  10.  
  11. # PROMPT_COMMAND runs before the prompt starts.
  12. # 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
  13. # 0 gets green, 1 gets red and everything else gets a mellow yellow.
  14. PROMPT_COMMAND='case $? in
  15. 0) exit_color=$green ;;
  16. 1) exit_color=$red ;;
  17. *) exit_color=$yellow ;;
  18. esac'
  19.  
  20. # Here I specify an array of colors used to give a non-0 job indicator a color.
  21. # So that means that when there are some jobs in the background, the job indicator will get colored.
  22. job_colors=( [0]="$bold$yellow" )
  23.  
  24. # PS1 depending if the user has root privileges or not.
  25. # 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)
  26. if (( UID > 0 ));
  27.  
  28. then PS1='\n┌───| ' # Start the prompt with a newline (\n)
  29.          PS1+='\[$bold$cyan\]\w\[$reset\] ]' # This puts the current working directory in bold cyan color, and resets the color to limit the coloring only to the current working dir definition.
  30.          PS1+='(\[$bold$green\]\u\[$reset\]@\[$blue\]\H\[$reset\])' # Here we do exactly the same but with different color and with username and hostname. We also put then inside parentheses for cosmetics.
  31.          PS1+='[$SHELL]' # This is simply the $SHELL variable that you can echo during interactive shell sessions. The square brackets are pure for cosmetics too. Yes, we can put such variables in the PS1 :)
  32.          PS1+='|[\[$white\]\A\[$reset\]][bn:\l]' # The time indicator (according to HH:MM formatting) in white color. Following that we have an indicator for the basename of the shell’s terminal device name.
  33.          PS1+='\[${job_colors[\j==0]}\][j:\j]\[$reset\]' # Let's break this down a bit.
  34.                 # You have the ${job_colors[\j==0]} array operation encased inside the non-printing character sequence indicators ( the \[ \] thingies, more info here - http://mywiki.wooledge.org/BashFAQ/053 )
  35.                 # 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'.
  36.                 # 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.
  37.                 # To not color any further than we need, we reset the color by issuing \[$reset\].
  38.          PS1+='â–º [\[$exit_color\]$?\[$reset\]]' # Exit status color colored by the $exit_color variable having been set in PROMPT_COMMAND above.
  39.          PS1+='\[$bold\]$(__git_ps1)\[$reset\] ' # Special git indicator, works only if you have git installed.
  40.          PS1+='\n└─\$ ' # End it with a newline again, so that you don't have the cursor next to the prompt but under it. Also it has the \$ prompt indicator at the end, which changes the prompt to $ when you're a normal user and # when you're the root user.
  41.          
  42.          # If the user is a root user (so has the UID 0), the following PS1 gets executed:
  43.                 else PS1='\n┌───| ' # Start the prompt with a newline (\n)
  44.                          PS1+='\[$bold$red\]\w\[$reset\] ]' # This puts the current working directory in bold RED color, and resets the color to limit the coloring only to the current working dir definition.
  45.                          PS1+='(\[$bold$red\]\u\[$reset\]@\[$blue\]\H\[$reset\])' # Here we do exactly the same but with different color and with username and hostname. We also put then inside parentheses for cosmetics.
  46.                          PS1+='[$SHELL]' # This is simply the $SHELL variable that you can echo during interactive shell sessions. The square brackets are pure for cosmetics too. Yes, we can put such variables in the PS1 :)
  47.                          PS1+='|[\[$white\]\A\[$reset\]][bn:\l]' # The time indicator (according to HH:MM formatting) in white color. Following that we have an indicator for the basename of the shell’s terminal device name.
  48.                          PS1+='\[${job_colors[\j==0]}\][j:\j]\[$reset\]' # Let's break this down a bit.
  49.                                 # You have the ${job_colors[\j==0]} array operation encased inside the non-printing character sequence indicators ( the \[ \] thingies, more info here - http://mywiki.wooledge.org/BashFAQ/053 )
  50.                                 # 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'.
  51.                                 # 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.
  52.                                 # To not color any further than we need, we reset the color by issuing \[$reset\].
  53.                          PS1+='â–º [\[$exit_color\]$?\[$reset\]]' # Exit status color colored by the $exit_color variable having been set in PROMPT_COMMAND above.
  54.                          PS1+='\[$bold\]$(__git_ps1)\[$reset\] ' # Special git indicator, works only if you have git installed.
  55.                          PS1+='\n└─\$ ' # End it with a newline again, so that you don't have the cursor next to the prompt but under it. Also it has the \$ prompt indicator at the end, which changes the prompt to $ when you're a normal user and # when you're the root user.
  56. fi

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