I can be kind of a nut when it comes to organizing certain things. If there is a way to minimize thought and/or processing, I will find and utilize it. One of the many reasons I love Linux/Open-Source is the freedom customize everything. By tweaking your bash prompt, one can promptly determine information about their working environment.

The prompt is generated from the contents variable PS1, which can be changed on the fly as one experiments.

## Default Prompt in Fedora
# PS1='[\u@\h: \W]\$ '
[username@host_name basedir]$ echo $PS1
[\u@\h \W]\$
[username@host_name basedir]$ PS1='[\W]\$ '
[basedir]$ PS1='[\u@\h: \W]\$ '
[username@host_name basedir]$

The escaped characters are expanded into properties of the system. For example, the default PS1 mentioned above is translated into: [username@hostname basedir]$ . For more information, RTFM: info bash and goto node “Printing Prompt” aka “6.9 Controlling the Prompt”.

lemma: I want to mention some configuration items for my preferred SCM tool: git . The color options really help with use of git in a terminal.

## Colorize git output for: branch, diff, interactive, status
$ git config --global color.ui auto
## Start gedit when text input is needed (commit message, etc)
$ git config --global core.editor gedit
## Set the merge-tool to meld (my preference) 
$ git config --global merge.tool meld

For more git configuration(s), consult the Git Pro book online .
What if you wanted something that’s not supported by the escaped characters? For example, wouldn’t it be nice to know what git branch you are on? The source of this feature is in git-prompt.sh .

How-to add git branch to prompt for single user or without sudo:
$ cp -v /usr/share/git-core/contrib/completion/git-prompt.sh $HOME/.git-prompt.sh
$ echo $'
source $HOME/.git-prompt.sh
PS1=\'[\\u@\\h: \\W$(__git_ps1 " (%s)")]\\$ \'' >> ~/.bashrc
## ... or just add the following lines to .bashrc (without the #)
# source $HOME/.git-prompt.sh
# PS1='[\u@\h: \W$(__git_ps1 " (%s)")]\$ '
How-to add git branch to prompt for all users (with sudo):
$ sudo cp -v /usr/share/git-core/contrib/completion/git-prompt.sh /etc/profile.d

If you keep in mind these files get parsed when you login, your prompt should look like: [username@hostname git_repo_dir (master)]$ . Now we have a bunch of useful text but not really presented in a useful way; this is where the color comes in. One can change the color of the output (even in scripts) with escape sequences. For example, the \e[34m sets the output to blue, which is followed by the text you want to echo (“Hello Blue World”) followed by a reset back to the default color environment \e[0m .

$ echo -e 'Hello \e[34m'"Blue"'\e[0m World'
Hello Blue World

Personally, I prefer to use a different method, which uses tput from the ncurses package because it looks cleaner.

$ echo -e "Hello $(tput setaf 4)Blue$(tput sgr0) World"
Hello Blue World

By default, Fedora is configured with a pallet of 256 terminal colors. The following code will print the full color map and perhaps provide some understanding of how the tput command is used.

for ((x=0; x<$(tput colors); x++));
  echo -e "${x}:\t$(tput setaf ${x})color\t$(tput bold)bold\t$(tput sgr0; tput setaf 0; tput setab ${x})background$(tput sgr0)"; 

Some of the subtleties of the command above: once you set a property its stays set and sgr0 resets all the properties. Since you have RTFM by this point, you know that any non-printing characters (the ones that set color) need to be surrounded with escaped brackets \[\] in a prompt. In other words, put escaped brackets around any tput command sequence.

tip: tput reset will reset all the terminal properties. While you’re playing around with the prompt ( PS1 ), you may need to reset everything.

Unfortunately, I sometimes have to work in a environment where Ubuntu is the Linux distribution used by the developers. In most cases, a chroot provides an easy clean way to: not be held hostage by the build environment. Consequently, I like to know what environment I’m building in so I put the chroot in my in my prompt, which is same format you would see in a mock chroot in shell mode.

.bashrc – colored prompt
if [ "x$debian_chroot" != "x" ]
## We're in a debian chroot
  echo -e "$(tput bold)CHROOT: $debian_chroot $(tput sgr0)"
  JAIL='<\[$(tput bold)\]${debian_chroot}\[$(tput sgr0)\]>'
## No Jail
if [ -z "$PS1" ]
  source $HOME/.git-prompt.sh
  if [ "$(tput colors)" = "256" ]
    PS1="${JAIL}[\[$(tput setaf 33)\]\u@\h \[$(tput bold;tput setaf 46)\]\W\[$(tput sgr0; tput setaf 15)\]\$(__git_ps1 \" (%s)\")\[$(tput sgr0)\]]\$ "
    PS1="${JAIL}[\[$(tput setaf 6)\]\u@\h \[$(tput setaf 2)\]\W\[\e[0;97m\]\$(__git_ps1 \" (%s)\")\[$(tput sgr0)\]]\$ "

These are the resulting prompt from add the above to .bashrc .

colored prompt
[username@hostname sandbox_dir]$ 
colored prompt inside a git repo directory
[username@hostname git_repo_dir (branchname)]$ 
colored prompt inside a debian chroot
<chrootname>[username@hostname chroot_dir]$ 

Now that you have the prompt color coded, its easier to parse for the information you need at the moment. I’ll leave you with one last tip: consider changing the root prompt to an attention color—like red—so you can reduce those late night mistakes as root…