- #!/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
- # Thanks to all the knowledgeable people in #bash @ irc.freenode.net
- # 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┌───| ' # Start the prompt with a newline (\n)
- 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.
- 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.
- 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 :)
- 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.
- PS1+='\[${job_colors[\j==0]}\][j:\j]\[$reset\]' # 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 \[ \] thingies, more info here - http://mywiki.wooledge.org/BashFAQ/053 )
- # 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\].
- PS1+='â–º [\[$exit_color\]$?\[$reset\]]' # Exit status color colored by the $exit_color variable having been set in PROMPT_COMMAND above.
- PS1+='\[$bold\]$(__git_ps1)\[$reset\] ' # Special git indicator, works only if you have git installed.
- 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.
- # If the user is a root user (so has the UID 0), the following PS1 gets executed:
- else PS1='\n┌───| ' # Start the prompt with a newline (\n)
- 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.
- 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.
- 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 :)
- 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.
- PS1+='\[${job_colors[\j==0]}\][j:\j]\[$reset\]' # 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 \[ \] thingies, more info here - http://mywiki.wooledge.org/BashFAQ/053 )
- # 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\].
- PS1+='â–º [\[$exit_color\]$?\[$reset\]]' # Exit status color colored by the $exit_color variable having been set in PROMPT_COMMAND above.
- PS1+='\[$bold\]$(__git_ps1)\[$reset\] ' # Special git indicator, works only if you have git installed.
- 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.
- fi
PS1_improved
Posted by Anonymous on Mon 26th Oct 2015 10:36
raw | new post
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.