15

Possible Duplicate:
How to customize .bashrc to configure command prompt?

When I run a command, I often times have trouble finding the beginning of the command output. An easy fix to this would be to color or bold the command prompt so that I can easily see where I left off. How?

  • In Ubuntu (and possibly Debian too,) uncomment the force_color_prompt=yes in .bashrc, or run the terminal with TERM=xterm-color. – alex Jul 06 '11 at 07:13

5 Answers5

19

Here's what I do. I use tput(1) instead of additional escape statements, because escape statements are hard for humans to read.

This is from my .bashrc

### Set the prompt like "username@hostname:~ $"
# See: http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/
# And: http://mywiki.wooledge.org/BashFAQ/037
# 'tput bold' will work regardless of the foreground and background colors.
# Place the tput output into variables, so they are only execd once.
bold=$(tput bold)
reset=$(tput sgr0)
export PS1="\u@\[$bold\]\h\[$reset\]:\w \$ "

Here's another alternative. This is much more readable then escape sequences.

# Bash
red=$(tput setaf 1)
green=$(tput setaf 2)
blue=$(tput setaf 4)
reset=$(tput sgr0)
PS1='\[$red\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\]\w\[$reset\]\$ '
Stefan Lasiewski
  • 19,754
  • 24
  • 70
  • 85
6

For ease of use, you can first map your colors in your .bashrc and then reuse it in your prompt variable ($PS1):

Step 1.: Map the colors:

#~/.bashrc

# Color mapping
grey='\[\033[1;30m\]'
red='\[\033[0;31m\]'
RED='\[\033[1;31m\]'
green='\[\033[0;32m\]'
GREEN='\[\033[1;32m\]'
yellow='\[\033[0;33m\]'
YELLOW='\[\033[1;33m\]'
purple='\[\033[0;35m\]'
PURPLE='\[\033[1;35m\]'
white='\[\033[0;37m\]'
WHITE='\[\033[1;37m\]'
blue='\[\033[0;34m\]'
BLUE='\[\033[1;34m\]'
cyan='\[\033[0;36m\]'
CYAN='\[\033[1;36m\]'
NC='\[\033[0m\]'

Step 2. Re-define your PS1 variable:

PS1="$yellow[$CYAN\t$yellow][$red\H$yellow][$GREEN\w$grey$yellow]$NC# "
jmaeder
  • 136
2

There is an excellent reference page describing how to colourize your bash prompt on the Arch Linux wiki.

It includes information about the colours, escape sequences and the correct way to include other characters or to print information in the prompt, such as the directory, host, etc.

As an example, a simple prompt like:

PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '

Can be broken down into these elements:

\[\e[1;32m] - an opening square bracket printed in green (1;32m)

[\u@h \W] - username@hostname and the basename of the current working directory

\$ - the prompt (a # if the UID is 0)

\[e[0m\] - the text reset escape signalling the end of the colour sequence.

Using these sequences, you can build up a colourful, informative prompt.

A word of caution: if you fail to correctly escape sequences, you can wreak havoc with your terminal's ability to print text.

jasonwryan
  • 73,126
2

I like the way Gentoo does it:

"\[\033[01;32m\]\u@\h\[\033[01;34m\] \W \$\[\033[00m\] "

Just add it to ~/.bashrc like so:

echo 'export PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \W \$\[\033[00m\] "' | tee -a ~/.bashrc

You can then log out of your terminal/console or just source ~/.bashrc

laebshade
  • 2,176
0

Here's mine from my .bashrc:

case $HOSTNAME in
    plato*) PSC="\e[1;33m" ;;
    *) PSC="\e[36m" ;;
esac

PS1="[\j]\[${PSC}\]\u@\h(\l) \[\e[37m\][ \w ]\[\e[00m\]\n\[\e[1m\]\#\[\e[0m\] \$ "

Edit to taste. This one also uses a different color for my main workstation vs. other hosts.

Keith
  • 7,914
  • 1
  • 28
  • 29