« back to all cards in this deck
Posted about 3 years ago. Visible to the public.

Customize your Bash prompt

The shell variable PS1 holds your bash prompt. You might want to change it to serve your needs best. Here is how to:

General

Information

  • \u: current username
  • \h: hostname up to the first ., \H: full hostname
  • \w: current working directory, \W: same, but only the basename
  • $(__git_ps1 "%s"): your current git branch if you're in a git directory, otherwise nothing
  • \$: if the effective UID is 0: #, otherwise $

  • \d: the date in "Weekday Month Date" format (e.g., "Tue May 26")
  • \t: the current time in 24-hour HH:MM:SS format, \T: same, but 12-hour format, \@: same, but in 12-hour am/pm format
  • \n: newline
  • \r: carriage return
  • \\: backslash

Colors & Styles

  • colors have to be escaped (see General), color codes should be followed by an m
  • put \[\e[‹code›m\] and \[\e[0m\] around the part of your prompt you want to style
  • where ‹code› is a ;-chain of:
    • x: attribute of the text
    • 3y: foreground color
    • 4y: background color
  • x:
    • 0: normal
    • 1: bold
    • 4: underline
    • 7: reverse
  • y is the color:
    • 0 black
    • 1 red
    • 2 green
    • 3 yellow
    • 4 blue
    • 5 magenta
    • 6 cyan
    • 7 white
  • example: \[\e[0;33m\]prompt:\[\e[0m\]

Save your settings by writing export PS1='...' to your .bashrc or .profile.

Examples

export PS1='\h \w$(__git_ps1 "(%s)") \$ ' => mycomputer ~/apps/chess/tmp(master) $ _ export PS1='\[\e[33m\]\h\[\e[0m\]:\W\[\e[33m\]$(__git_ps1 "(%s)")\[\e[0m\] \u\$ ' => mycomputer:tmp(master) tom$ _ # Henning's awesome TRON prompt 2.0.2 with current Git branch and success state of the last command (the syntax coloring here does not do it justice): export PS1='`if [ $? = 0 ]; then echo "\[\033[01;32m\]✔"; else echo "\[\033[01;31m\]✘"; fi` \[\033[01;30m\]\h\[\033[01;34m\] \w\[\033[35m\]$(__git_ps1 " %s") \[\033[01;30m\]>\[\033[00m\] ' => ✔ mycomputer ~/projects/platforms master > _ # Arne's epic timestamped prompt with return status indicator and status-colored (green if fresh, or red if unstaged, or yellow if staged) git branch: export PS1='\[\e[01;30m\]\t`if [ $? = 0 ]; then echo "\[\e[32m\] ✔ "; else echo "\[\e[31m\] ✘ "; fi`\[\e[00;37m\]\u\[\e[01;37m\]:`[[ $(git status 2> /dev/null | head -n2 | tail -n1) != "# Changes to be committed:" ]] && echo "\[\e[31m\]" || echo "\[\e[33m\]"``[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] || echo "\[\e[32m\]"`$(__git_ps1 "(%s)\[\e[00m\]")\[\e[01;34m\]\w\[\e[00m\]\$ ' => 03:13:37 ✔ arne:(master)~/projects/geordi$ _ # Arne's danger prompt for root use. No git information and alarming red and yellow (which you can't see here): export PS1='\[\e[01;30m\]\t \[\e[31m\]\u\[\e[37m\]:\[\e[33m\]\w\[\e[31m\]\$\[\033[00m\] ' => 03:13:37 root:/tmp/foo# _ # Kim's mix of those above with small changes. TRON promt with timestamp, return status indicator, hostname, git informations and working directory (but all non-bold). export PS1='\[\033[01;30m\]\t `if [ $? = 0 ]; then echo "\[\033[01;32m\]ツ"; else echo "\[\033[01;31m\]✗"; fi` \[\033[00;32m\]\h\[\033[00;37m\]:\[\033[31m\]$(__git_ps1 "(%s)\[\033[01m\]")\[\033[00;34m\]\w\[\033[00m\] >' => 03:13:37 ツ mycomputer:(master)~/code/foo > # Kim's root prompt (the same as above without git and the hostname is red) export PS1='\[\033[01;30m\]\t `if [ $? = 0 ]; then echo "\[\033[01;32m\]ツ"; else echo "\[\033[01;31m\]✗"; fi` \[\033[00;31m\]\h\[\033[00;37m\]:\[\033[00;34m\]\w\[\033[00m\] >' => 03:13:37 ツ mycomputer:~/code/foo > # Martin's root prompt export PS1='`if [ $? = 0 ]; then echo "\[\033[01;32m\]✔"; else echo "\[\033[01;31m\]✘"; fi` \[\033[01;30m\]\h\[\033[01;34m\] \w\[\033[35m\]$(__git_ps1 " %s") \[\033[01;31m\]\n>\[\033[00m\] '

See the attached screenshot for colorful examples of the above prompt settings (the user is arne on machine happycat in directory ~/code/geordi on the master branch).

Author of this card:

Avatar
Dominik Schoeler
Attachments:
bash-prompt-examples.png
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
Posted by Dominik Schoeler to makandropedia