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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9dcdaccdbc7c8c4cce9c1c6daddc7c8c4cc">[email protected]</a>:~ $" # 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="<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b1e2b">[email protected]</a>[$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]$ '
Method 2
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='[33[1;30m]' red='[33[0;31m]' RED='[33[1;31m]' green='[33[0;32m]' GREEN='[33[1;32m]' yellow='[33[0;33m]' YELLOW='[33[1;33m]' purple='[33[0;35m]' PURPLE='[33[1;35m]' white='[33[0;37m]' WHITE='[33[1;37m]' blue='[33[0;34m]' BLUE='[33[1;34m]' cyan='[33[0;36m]' CYAN='[33[1;36m]' NC='[33[0m]'
Step 2. Re-define your PS1 variable:
PS1="$yellow[$CYANt$yellow][$redH$yellow][$GREENw$grey$yellow]$NC# "
Method 3
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][<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="770237">[email protected]</a>h W]$[e[0m] '
Can be broken down into these elements:
[e[1;32m] – an opening square bracket printed in green (1;32m)
[[email protected] W] – [email protected] 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.
Method 4
I like the way Gentoo does it:
"[33[01;32m]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="710431">[email protected]</a>h[33[01;34m] W $[33[00m] "
Just add it to ~/.bashrc like so:
echo 'export PS1="[33[01;32m]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f7c2">[email protected]</a>h[33[01;34m] W $[33[00m] "' | tee -a ~/.bashrc
You can then log out of your terminal/console or just source ~/.bashrc
Method 5
Here’s mine from my .bashrc:
case $HOSTNAME in
plato*) PSC="e[1;33m" ;;
*) PSC="e[36m" ;;
esac
PS1="[j][${PSC}]<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa8fba">[email protected]</a>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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0