SHARE
TWEET

Custom Bash Prompt

Sir-Shadow May 13th, 2014 (edited) 464 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #------------------------------------------------------------
  2. #------------------------------------------------------------
  3. #--------------------Custom bash prompt----------------------
  4. #------------------------------------------------------------
  5. #------------------------------------------------------------
  6.  
  7.  
  8. # you can copy all these lines directly to .bashrc but I recommend to
  9. # make a script file and call it from .bashrc like the below line
  10. # source ~/.my-scripts/bash-prompt.sh
  11.  
  12. #colors
  13. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14. # foreground   background   color
  15. # 30 - - - - - 40 - - - - - # black
  16. # 31 - - - - - 41 - - - - - # red
  17. # 32 - - - - - 42 - - - - - # green
  18. # 33 - - - - - 43 - - - - - # yellow
  19. # 34 - - - - - 44 - - - - - # blue
  20. # 35 - - - - - 45 - - - - - # magenta
  21. # 36 - - - - - 46 - - - - - # cyan
  22. # 37 - - - - - 47 - - - - - # white
  23.  
  24. # 00 - - - - - # dark
  25. # 01 - - - - - # light
  26.  
  27.  
  28. # special characters
  29. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  30. # \a : an ASCII bell character (07)
  31. # \d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
  32. # \D : \D{format}       the format is passed to strftime(3) and the result is
  33. #      inserted into the prompt string; an empty format results in a
  34. #      locale-specific time representation. The braces are required
  35. # \e : an ASCII escape character (033)
  36. # \h : the host name up to the first '.'
  37. # \H : the host name
  38. # \j : the number of jobs currently managed by the shell
  39. # \l : the base name of the shell’s terminal device name
  40. # \n : newline
  41. # \r : carriage return
  42. # \s : the name of the shell, the base name of $0 (the portion following the final slash)
  43. # \t : the current time in 24-hour HH:MM:SS format
  44. # \T : the current time in 12-hour HH:MM:SS format
  45. # \@ : the current time in 12-hour am/pm format
  46. # \A : the current time in 24-hour HH:MM format
  47. # \u : the username of the current user
  48. # \v : the version of bash (e.g., 2.00)
  49. # \V : the release of bash, version + patch level (e.g., 2.00.0)
  50. # \w : the current working directory, with $HOME abbreviated with a tilde
  51. # \W : the base name of the current working directory, with $HOME abbreviated with a tilde
  52. # \! : the history number of this command
  53. # \# : the command number of this command
  54. # \$ : if the effective UID is 0, a #, otherwise a $
  55. # \\ : a backslash
  56. # \[ : begin a sequence of non-printing characters, which could be used to
  57. #      embed a terminal control sequence into the prompt
  58. # \] : end a sequence of non-printing characters
  59. # \nnn : the character corresponding to the octal number nnn
  60.  
  61.  
  62. # Making a line of '-' character for separating prompts
  63. # "tput cols" returns width of command line window.
  64. _ps1_line() {
  65.     echo -n $(
  66.     for(( i=1; i<=$( tput cols ); i++))
  67.     do
  68.         echo -n "-"
  69.     done
  70.     )
  71. }
  72.  
  73. # Colorizing Prompt
  74. # $1 for background, $3 for foreground and $2 for making colour light or dark
  75. _ps1_colour() {
  76.     echo -n "\[\033[$1;$2;$3m\]"
  77. }
  78.  
  79. # Clearing color to default
  80. _ps1_colour_clear() {
  81.     echo -n "\[\033[00m\]"
  82. }
  83.  
  84. # Making text bold
  85. _ps1_bold() {
  86.     echo -n "\[$(tput bold)\]"
  87. }
  88.  
  89. # Clearing tput format. here we use it for clearing bold.
  90. _ps1_bold_clear() {
  91.     echo -n "\[$(tput sgr0)\]"
  92. }
  93.  
  94. # Making an array of what will be printed in each line. (each element of array for 1 line)
  95. _update_ps1() {
  96.     local parts=(
  97.         "$(_ps1_colour 00 01 36)$(_ps1_line)"
  98.         "$(_ps1_colour 00 01 34)USER$(_ps1_colour 00 01 33) ›››› $(_ps1_colour 00 01 34)\u"
  99.         "$(_ps1_colour 00 01 34)HOST$(_ps1_colour 00 01 33) ›››› $(_ps1_colour 00 01 34)\h : $(_ps1_colour 00 01 33)\w"
  100.         "$(_ps1_colour 00 01 36)››››$(_ps1_colour_clear)"
  101.     )
  102.    
  103.     # After of generating array, it should be returned. it could be like this "echo -n "${parts[@]}"".
  104.     # The problem is that it returns an space character between each element.
  105.     # '\r' would be useful but it have side effect to prompt. it makes frozen characters.
  106.     local length=${#parts[@]}
  107.     length=$length-1
  108.  
  109.     echo -n $(
  110.     for(( i=0; i<=$length; i++))
  111.     do
  112.         echo -n ${parts[$i]}
  113.  
  114.         # here we need to go to next line. but not for the last line.
  115.         if (( $i < $length ))
  116.         then
  117.             echo -n "\n"
  118.         fi
  119.     done
  120.     )
  121. }
  122.  
  123. # initializing bash prompt variable
  124. export PS1="${debian_chroot:+($debian_chroot)}$(_update_ps1) "
  125.  
  126. #------------------------------------------------------------
  127. #------------------------------------------------------------
  128. #-----------------End of Custom bash prompt------------------
  129. #------------------------------------------------------------
  130. #------------------------------------------------------------
RAW Paste Data
Top