Linux Bash Colors
Linux Bash Colors
https://wiki.archlinux.org/index.php/Color_Bash_...
There are a variety of possibilities for Bash's prompt (PS1), and customizing it can help your productivity at the
command line. Adding additional information and colors can make it the prompt to stand out.
Bash
This guide shows how to customize the personal prompt of a regular user.
Environment variables
Contents
1 Applying changes
2 Basic prompts
2.1 Regular user
2.2 Root
3 Slightly fancier prompts
3.1 Regular user
3.2 Root
4 Advanced prompts
4.1 Load/Mem Status for 256colors
4.2 List of colors for prompt and Bash
4.3 Prompt escapes
4.4 Positioning the cursor
4.5 Return value visualisation
5 Tips and tricks
5.1 Dierent colors for text entry and console output
5.2 Random quotations at logon
5.3 Colorized Arch latest news at logon
5.4 Colors overview
5.5 Colorized git prompt
6 See also
Applying changes
To apply changes from this article to your
.bashrc
$ source ~/.bashrc
Basic prompts
The following settings are useful for distinguishing the root prompt from non-root users.
Regular user
A green prompt for regular users:
[chiri@zetsubou ~]$ _
~/.bashrc
#PS1='[\u@\h \W]\$ ' # Default
PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '
Root
A red prompt for root (copy from
/etc/skel/ ,
if not already):
[root@zetsubou ~]# _
/root/.bashrc
#PS1='[\u@\h \W]\$ ' # Default
PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\] '
This will give a very pleasing, colorful prompt and theme for the console with bright white text.
The string above contains color-set escape sequences (start coloring:
\[\e[color\] ,
end coloring:
\[\e[m\]
\u - Username. The original prompt also has \h, which prints the host name.
\w - Current absolute path. Use \W for current relative path.
\$ - The prompt character (eg. # for root, $ for regular users).
The last color-set sequence, \[\e[1;37m\] , is not closed, so the remaining text (everything typed into the terminal, program output and so on) will be in that (bright
white) color. It may be desirable to change this color, or to delete the last escape sequence in order to leave the actual output in unaltered color.
Root
A red/blue prompt for root:
root ~/docs # echo "sample output text"
1 of 5
14-10-2014 19:28
https://wiki.archlinux.org/index.php/Color_Bash_...
This will give you a red designation and green console text.
Advanced prompts
Load/Mem Status for 256colors
This is not even pushing the limits. Other than using sed to parse the memory and load average (using the -u option for non-buering), and the builtin history to
save your history to your HISTFILE after every command, which you may nd incredibly useful when dealing with crashing shells or subshells, this is essentially just
making BASH print variables it already knows, making this extremely fast compared to prompts with non-builtin commands.
This prompt is from AskApache.com's BASH Power Prompt article (http://www.askapache.com/linux-unix/bash-power-prompt.html), which goes into greater detail.
It is especially helpful for those wanting to understand 256 color terminals, ncurses, termcap, and terminfo.
This is for 256 color terminals, which is where the
\033[38;5;22m
802/1024MB
1.28 1.20 1.13 3/94 18563
[5416:17880 0:70] 05:35:50 Wed Apr 21 [[email protected]:/dev/pts/0 +1] ~
(1:70)$ _
PROMPT_COMMAND='history -a;echo -en "\033[m\033[38;5;2m"$(( $(sed -nu "s/MemFree:[\t ]\+\([0-9]\+\) kB/\1/p" /proc/meminfo)/1024))"\033[38;5;22m/"$(($(sed -nu "s/MemTotal:[\t ]\+\([0-9]\+\) kB/\1/Ip" /proc/meminfo)/1024 ))MB
PS1='\[\e[m\n\e[1;30m\][$$:$PPID \j:\!\[\e[1;30m\]]\[\e[0;36m\] \T \d \[\e[1;30m\][\[\e[1;34m\]\u@\H\[\e[1;30m\]:\[\e[0;37m\]${SSH_TTY} \[\e[0;32m\]+${SHLVL}\[\e[1;30m\]] \[\e[1;37m\]\w\[\e[0;37m\] \n($SHLVL:\!)\$ '
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
Black - Regular
Red
Green
Yellow
Blue
Purple
Cyan
White
Black - Bold
Red
Green
Yellow
Blue
Purple
Cyan
White
Black - Underline
Red
Green
Yellow
Blue
Purple
Cyan
White
Black - Background
Red
Green
Yellow
Blue
Purple
Cyan
White
Text Reset
Or if you prefer color names you will know how to spell without a special decoder ring and want high intensity colors:
2 of 5
# Reset
Color_Off='\e[0m'
# Text Reset
# Regular Colors
Black='\e[0;30m'
Red='\e[0;31m'
Green='\e[0;32m'
Yellow='\e[0;33m'
Blue='\e[0;34m'
Purple='\e[0;35m'
Cyan='\e[0;36m'
White='\e[0;37m'
#
#
#
#
#
#
#
#
Black
Red
Green
Yellow
Blue
Purple
Cyan
White
# Bold
BBlack='\e[1;30m'
BRed='\e[1;31m'
BGreen='\e[1;32m'
BYellow='\e[1;33m'
BBlue='\e[1;34m'
BPurple='\e[1;35m'
BCyan='\e[1;36m'
BWhite='\e[1;37m'
#
#
#
#
#
#
#
#
Black
Red
Green
Yellow
Blue
Purple
Cyan
White
# Underline
UBlack='\e[4;30m'
URed='\e[4;31m'
UGreen='\e[4;32m'
UYellow='\e[4;33m'
UBlue='\e[4;34m'
UPurple='\e[4;35m'
UCyan='\e[4;36m'
UWhite='\e[4;37m'
#
#
#
#
#
#
#
#
Black
Red
Green
Yellow
Blue
Purple
Cyan
White
# Background
On_Black='\e[40m'
On_Red='\e[41m'
On_Green='\e[42m'
On_Yellow='\e[43m'
On_Blue='\e[44m'
On_Purple='\e[45m'
On_Cyan='\e[46m'
On_White='\e[47m'
#
#
#
#
#
#
#
#
Black
Red
Green
Yellow
Blue
Purple
Cyan
White
# High Intensity
IBlack='\e[0;90m'
IRed='\e[0;91m'
IGreen='\e[0;92m'
IYellow='\e[0;93m'
IBlue='\e[0;94m'
IPurple='\e[0;95m'
ICyan='\e[0;96m'
IWhite='\e[0;97m'
#
#
#
#
#
#
#
#
Black
Red
Green
Yellow
Blue
Purple
Cyan
White
#
#
#
#
#
#
Black
Red
Green
Yellow
Blue
Purple
14-10-2014 19:28
https://wiki.archlinux.org/index.php/Color_Bash_...
# Cyan
# White
-e "${txtblu}test"
-e "${bldblu}test"
-e "${undblu}test"
-e "${bakblu}test"
$color
\[ \]
escapes around them make them not count as character positions and the cursor position is not
Note: If experiencing premature line wrapping when entering commands, then missing escapes ( \[
\] )
Prompt escapes
The various Bash prompt escapes listed in the manpage:
Bash allows these prompt strings to be customized by inserting a
number of backslash-escaped special characters that are
decoded as follows:
\a
\d
\D{format}
\e
\h
\H
\j
\l
\n
\r
\s
\t
\T
\@
\A
\u
\v
\V
\w
\W
\!
\#
\$
\nnn
\\
\[
\]
The command number and the history number are usually different:
the history number of a command is its position in the history
list, which may include commands restored from the history file
(see HISTORY below), while the command number is the position in
the sequence of commands executed during the current shell session.
After the string is decoded, it is expanded via parameter
expansion, command substitution, arithmetic expansion, and quote
removal, subject to the value of the promptvars shell option (see
the description of the shopt command under SHELL BUILTIN COMMANDS
below).
The following example uses these sequences to display the time in the upper right corner:
PS1=">\[\033[s\]\[\033[1;\$((COLUMNS-4))f\]\$(date +%H:%M)\[\033[u\]"
The environment variable COLUMNS contains the number of columns of the terminal. The above example substracts 4 from its value in order to justify the ve
character wide output of date at the right border.
3 of 5
14-10-2014 19:28
https://wiki.archlinux.org/index.php/Color_Bash_...
PS1="\$? \$(if [[ \$? == 0 ]]; then echo \"\[\033[0;32m\];)\"; else echo \"\[\033[0;31m\];(\"; fi)\[\033[00m\] : "
Zero is a green smiley and non-zero a red one. So your prompt will smile if the last operation was successful.
But you will probably want to include the username and hostname as well, like this:
0 ;) andy@alba ~ $ true
0 ;) andy@alba ~ $ false
1 ;( andy@alba ~ $ _
Or, if you want, you can build your prompt using the unicode symbol for a zero status and the unicode symbol for a nonzero status:
0 andy@alba ~ $ true
0 andy@alba ~ $ false
1 andy@alba ~ $ I\ will\ try\ to\ type\ a\ wrong\ command...
bash: I will try to type a wrong command...: command not found
127 andy@alba ~ $ _
PROMPT_COMMAND
set_prompt () {
Last_Command=$? # Must come first!
Blue='\[\e[01;34m\]'
White='\[\e[01;37m\]'
Red='\[\e[01;31m\]'
Green='\[\e[01;32m\]'
Reset='\[\e[00m\]'
FancyX='\342\234\227'
Checkmark='\342\234\223'
# Add a bright white exit status for the last command
PS1="$White\$? "
# If it was successful, print a green check mark. Otherwise, print
# a red X.
if [[ $Last_Command == 0 ]]; then
PS1+="$Green$Checkmark "
else
PS1+="$Red$FancyX "
fi
# If root, just print the host in red. Otherwise, print the current user
# and host in green.
if [[ $EUID == 0 ]]; then
PS1+="$Red\\h "
else
PS1+="$Green\\u@\\h "
fi
# Print the working directory and prompt marker in blue, and reset
# the text color to the default.
PS1+="$Blue\\w \\\$$Reset "
}
PROMPT_COMMAND='set_prompt'
4 of 5
14-10-2014 19:28
https://wiki.archlinux.org/index.php/Color_Bash_...
[ https://www.archlinux.org/news/november-release-of-install-media-available/ ]
~/.bashrc
# Arch latest news
if [ "$PS1" ] && [[ $(ping -c1 www.google.com 2>&-) ]]; then
# The characters ", " are used as metacharacters. They should not be encountered in a feed...
echo -e "$(echo $(curl --silent https://www.archlinux.org/feeds/news/ | sed -e ':a;N;$!ba;s/\n/ /g') | \
sed -e 's/&/\&/g
s/<\|</</g
s/>\|>/>/g
s/<\/a>//g
s/href\=\"//g
s/<title>/\\n\\n\\n
:: \\e[01;31m/g; s/<\/title>/\\e[00m ::\\n/g
s/<link>/ [ \\e[01;36m/g; s/<\/link>/\\e[00m ]/g
s/<description>/\\n\\n\\e[00;37m/g; s/<\/description>/\\e[00m\\n\\n/g
s/<p\( [^>]*\)\?>\|<br\s*\/\?>/\n/g
s/<b\( [^>]*\)\?>\|<strong\( [^>]*\)\?>/\\e[01;30m/g; s/<\/b>\|<\/strong>/\\e[00;37m/g
s/<i\( [^>]*\)\?>\|<em\( [^>]*\)\?>/\\e[41;37m/g; s/<\/i>\|<\/em>/\\e[00;37m/g
s/<u\( [^>]*\)\?>/\\e[4;37m/g; s/<\/u>/\\e[00;37m/g
s/<code\( [^>]*\)\?>/\\e[00m/g; s/<\/code>/\\e[00;37m/g
s/<a[^|t]*\([^\"]*\)\"[^>]*>\([^]*\)[^]*/\\e[01;31m\2\\e[00;37m \\e[01;34m[\\e[00;37m \\e[04m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g
s/<li\( [^>]*\)\?>/\n \\e[01;34m*\\e[00;37m /g
s/<!\[CDATA\[\|\]\]>//g
s/\|>\s*<//g
s/ *<[^>]\+> */ /g
s/[<>]//g')\n\n";
fi
Colors overview
The page at http://ascii-table.com/ansi-escape-sequences.php describes the various available color escapes. The following Bash function displays a table with
ready-to-copy escape codes.
~/.bashrc
colors() {
local fgc bgc vals seq0
printf
printf
printf
printf
%s\n" '\e[${value};...;${value}m'
\e[33mforeground colors\e[m\n"
\e[43mbackground colors\e[m\n"
\e[1mbold-faced look\e[m\n\n"
# foreground colors
for fgc in {30..37}; do
# background colors
for bgc in {40..47}; do
fgc=${fgc#37} # white
bgc=${bgc#40} # black
vals="${fgc:+$fgc;}${bgc}"
vals=${vals%%;}
seq0="${vals:+\e[${vals}m}"
printf " %-9s" "${seq0:-(default)}"
printf " ${seq0}TEXT\e[m"
printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
done
echo; echo
done
}
/usr/share/git/completion/git-prompt.sh
~/.bashrc
source /usr/share/git/completion/git-prompt.sh
and use
__git_ps1
inside
PS1
or
PROMPT_COMMAND
See also
If you want to create a style all your own, you can take a look at these tips:
Community examples and screenshots in the Forum thread: What's your PS1? (https://bbs.archlinux.org/viewtopic.php?id=50885)
The original not modied Gentoo's /etc/bash.bashrc le can be found here (http://www.jeremysands.com/archlinux/gentoo-bashrc-2008.0) [dead link 2014-08-05].
See also the gentoo-bashrc (https://aur.archlinux.org/packages/gentoo-bashrc/) package from AUR.
tput(1)
tput reference on bash-hackers.org (http://wiki.bash-hackers.org/scripting/terminalcodes)
Colours and Cursor Movement With tput (http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html)
Nice Xresources color schemes collection (http://xcolors.net/)
Bash Prompt HOWTO (http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html)
Giles Orr's collection of sample prompts (http://gilesorr.com/bashprompt/prompts/index.html)
Bash tips: Colors and formatting (http://misc.ogisoft.com/bash/tip_colors_and_formatting)
Retrieved from "https://wiki.archlinux.org/index.php?title=Color_Bash_Prompt&oldid=335899"
Categories: Eye candy Command shells
This page was last modied on 17 September 2014, at 21:20.
Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.
5 of 5
14-10-2014 19:28