To apply changes made in ~/.bashrc
without restarting your shell, source the script.
The following settings are useful for distinguishing the root prompt from non-root users.
A green prompt for regular users:
~/.bashrc
#PS1='[\u@\h \W]\$ ' # Default PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '
A red prompt for root (copy from /etc/skel/
, if not already):
/root/.bashrc
#PS1='[\u@\h \W]\$ ' # Default PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\] '
The following settings provides more professional prompts.
A green/blue prompt for regular users:
PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$\[\e[m\] \[\e[1;37m\]'
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\]
) and information placeholders:
#
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.
A red/blue prompt for root:
PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[0;31m\]\$ \[\e[m\]\[\e[0;32m\]'
This will give you a red designation and green console text.
This is not even pushing the limits. Other than using sed
to parse the memory and load average (using the -u
option for non-buffering), and the builtin history to save your history to your HISTFILE
after every command, which you may find 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, 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
terminal escapes come from.
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"\t\033[m\033[38;5;55m$(< /proc/loadavg)\033[m"' 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:\!)\$ '
Add this to your Bash file(s) to define colors for prompt and commands:
txtblk='\e[0;30m' # Black - Regular txtred='\e[0;31m' # Red txtgrn='\e[0;32m' # Green txtylw='\e[0;33m' # Yellow txtblu='\e[0;34m' # Blue txtpur='\e[0;35m' # Purple txtcyn='\e[0;36m' # Cyan txtwht='\e[0;37m' # White bldblk='\e[1;30m' # Black - Bold bldred='\e[1;31m' # Red bldgrn='\e[1;32m' # Green bldylw='\e[1;33m' # Yellow bldblu='\e[1;34m' # Blue bldpur='\e[1;35m' # Purple bldcyn='\e[1;36m' # Cyan bldwht='\e[1;37m' # White unkblk='\e[4;30m' # Black - Underline undred='\e[4;31m' # Red undgrn='\e[4;32m' # Green undylw='\e[4;33m' # Yellow undblu='\e[4;34m' # Blue undpur='\e[4;35m' # Purple undcyn='\e[4;36m' # Cyan undwht='\e[4;37m' # White bakblk='\e[40m' # Black - Background bakred='\e[41m' # Red bakgrn='\e[42m' # Green bakylw='\e[43m' # Yellow bakblu='\e[44m' # Blue bakpur='\e[45m' # Purple bakcyn='\e[46m' # Cyan bakwht='\e[47m' # White txtrst='\e[0m' # Text Reset
Or if you prefer color names you will know how to spell without a special decoder ring and want high intensity colors:
# Reset Color_Off='\e[0m' # Text Reset # Regular Colors Black='\e[0;30m' # Black Red='\e[0;31m' # Red Green='\e[0;32m' # Green Yellow='\e[0;33m' # Yellow Blue='\e[0;34m' # Blue Purple='\e[0;35m' # Purple Cyan='\e[0;36m' # Cyan White='\e[0;37m' # White # Bold BBlack='\e[1;30m' # Black BRed='\e[1;31m' # Red BGreen='\e[1;32m' # Green BYellow='\e[1;33m' # Yellow BBlue='\e[1;34m' # Blue BPurple='\e[1;35m' # Purple BCyan='\e[1;36m' # Cyan BWhite='\e[1;37m' # White # Underline UBlack='\e[4;30m' # Black URed='\e[4;31m' # Red UGreen='\e[4;32m' # Green UYellow='\e[4;33m' # Yellow UBlue='\e[4;34m' # Blue UPurple='\e[4;35m' # Purple UCyan='\e[4;36m' # Cyan UWhite='\e[4;37m' # White # Background On_Black='\e[40m' # Black On_Red='\e[41m' # Red On_Green='\e[42m' # Green On_Yellow='\e[43m' # Yellow On_Blue='\e[44m' # Blue On_Purple='\e[45m' # Purple On_Cyan='\e[46m' # Cyan On_White='\e[47m' # White # High Intensity IBlack='\e[0;90m' # Black IRed='\e[0;91m' # Red IGreen='\e[0;92m' # Green IYellow='\e[0;93m' # Yellow IBlue='\e[0;94m' # Blue IPurple='\e[0;95m' # Purple ICyan='\e[0;96m' # Cyan IWhite='\e[0;97m' # White # Bold High Intensity BIBlack='\e[1;90m' # Black BIRed='\e[1;91m' # Red BIGreen='\e[1;92m' # Green BIYellow='\e[1;93m' # Yellow BIBlue='\e[1;94m' # Blue BIPurple='\e[1;95m' # Purple BICyan='\e[1;96m' # Cyan BIWhite='\e[1;97m' # White # High Intensity backgrounds On_IBlack='\e[0;100m' # Black On_IRed='\e[0;101m' # Red On_IGreen='\e[0;102m' # Green On_IYellow='\e[0;103m' # Yellow On_IBlue='\e[0;104m' # Blue On_IPurple='\e[0;105m' # Purple On_ICyan='\e[0;106m' # Cyan On_IWhite='\e[0;107m' # White
To use in commands from your shell environment:
PS1="\[$txtblu\]foo\[$txtred\] bar\[$txtrst\] baz : "
Double quotes enable $color
variable expansion and the \[ \]
escapes around them make them not count as character positions and the cursor position is not wrong.
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 an ASCII bell character (07) \d the date in "Weekday Month Date" format (e.g., "Tue May 26") \D{format} the format is passed to strftime(3) and the result is inserted into the prompt string an empty format results in a locale-specific time representation. The braces are required \e an ASCII escape character (033) \h the hostname up to the first `.' \H the hostname \j the number of jobs currently managed by the shell \l the basename of the shell's terminal device name \n newline \r carriage return \s the name of the shell, the basename of $0 (the portion following the final slash) \t the current time in 24-hour HH:MM:SS format \T the current time in 12-hour HH:MM:SS format \@ the current time in 12-hour am/pm format \A the current time in 24-hour HH:MM format \u the username of the current user \v the version of bash (e.g., 2.00) \V the release of bash, version + patch level (e.g., 2.00.0) \w the current working directory, with $HOME abbreviated with a tilde \W the basename of the current working directory, with $HOME abbreviated with a tilde \! the history number of this command \# the command number of this command \$ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn \\ a backslash \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt \] end a sequence of non-printing characters 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 sequence sets the cursor position:
\[\033[;
f\]
The current cursor position can be saved using:
\[\033[s\]
To restore a position, use the following sequence:
\[\033[u\]
The following example uses these sequences to display the time in the upper right corner:
PS1=">\[\033[s\]\[\033[1;\$((COLUMNS-5))f\]\$(date +%H:%M)\[\033[u\]"
The environment variable COLUMNS
contains the number of columns of the terminal. The above example substracts 5 from its value in order to justify the five-character wide output of date
at the right border.
Use the following prompt to see the return value of last command:
#return value visualisation 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:
#return value visualisation PS1="\[\033[01;37m\]\$? \$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\];)\"; else echo \"\[\033[01;31m\];(\"; fi) $(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]\h'; else echo '\[\033[01;32m\]\u@\h'; fi)\[\033[01;34m\] \w \$\[\033[00m\] "
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:
#return value visualisation PS1="\[\033[01;37m\]\$? \$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\]\342\234\223\"; else echo \"\[\033[01;31m\]\342\234\227\"; fi) $(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]\h'; else echo '\[\033[01;32m\]\u@\h'; fi)\[\033[01;34m\] \w \$\[\033[00m\] "
Alternatively, this can be made more readable with 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'
Here is an alternative that only includes the error status, if non-zero:
PROMPT_COMMAND='es=$?; [[ $es -eq 0 ]] && unset error || error=$(echo -e "\e[1;41m $es \e[40m")' PS1="${error} ${PS1}"
If you do not reset the text color at the end of your prompt, both the text you enter and the console text will simply stay in that color. If you want to edit text in a special color but still use the default color for command output, you will need to reset the color after you press Enter
, but still before any commands get run. You can do this by installing a DEBUG trap, like this:
~/.bashrc
trap 'printf "\e[0m" "$_"' DEBUG
For a brown Fortune prompt, add:
~/.bashrc
[[ "$PS1" ]] && echo -e "\e[00;33m$(/usr/bin/fortune)\e[00m"
To read 10 latest news items from the Arch official website, user grufo has written a small and coloured RSS escaping script (scrollable):
~/.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/<\/a>/£/g s/href\=\"/§/g s//\\n\\n\\n :: \\e[01;31m/g; s/<\/title>/\\e[00m ::\\n/g s// [ \\e[01;36m/g; s/<\/link>/\\e[00m ]/g s/ /\\n\\n\\e[00;37m/g; s/<\/description>/\\e[00m\\n\\n/g s/ ]*\)\?>\|
/\n/g s/]*\)\?>\|]*\)\?>/\\e[01;30m/g; s/<\/b>\|<\/strong>/\\e[00;37m/g s/]*\)\?>\|]*\)\?>/\\e[41;37m/g; s/<\/i>\|<\/em>/\\e[00;37m/g s/]*\)\?>/\\e[4;37m/g; s/<\/u>/\\e[00;37m/g s/]*\)\?>/\\e[00m/g; s/<\/code>/\\e[00;37m/g s/]*>\([^£]*\)[^£]*£/\\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/
]*\)\?>/\n \\e[01;34m*\\e[00;37m /g s///g s/\|>\s*/g s/ *<[^>]\+> */ /g s/[<>£§]//g')\n\n"; fi
To only get the absolute latest item, use this:
# 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/ | awk ' NR == 1 {while ($0 !~ /<\/item>/) {print;getline} sub(/<\/item>.*/,"") ;print}' | sed -e ':a;N;$!ba;s/\n/ /g') | \ sed -e 's/&/\&/g s/<\|<//g s/<\/a>/£/g s/href\=\"/§/g s//\\n\\n\\n :: \\e[01;31m/g; s/<\/title>/\\e[00m ::\\n/g s// [ \\e[01;36m/g; s/<\/link>/\\e[00m ]/g s/ /\\n\\n\\e[00;37m/g; s/<\/description>/\\e[00m\\n\\n/g s/ ]*\)\?>\|
/\n/g s/]*\)\?>\|]*\)\?>/\\e[01;30m/g; s/<\/b>\|<\/strong>/\\e[00;37m/g s/]*\)\?>\|]*\)\?>/\\e[41;37m/g; s/<\/i>\|<\/em>/\\e[00;37m/g s/]*\)\?>/\\e[4;37m/g; s/<\/u>/\\e[00;37m/g s/]*\)\?>/\\e[00m/g; s/<\/code>/\\e[00;37m/g s/]*>\([^£]*\)[^£]*£/\\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/
]*\)\?>/\n \\e[01;34m*\\e[00;37m /g s///g s/\|>\s*/g s/ *<[^>]\+> */ /g s/[<>£§]//g')\n\n"; fi
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 "Color escapes are %s\n" '\e[${value};...;${value}m' printf "Values 30..37 are \e[33mforeground colors\e[m\n" printf "Values 40..47 are \e[43mbackground colors\e[m\n" printf "Value 1 gives a \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 }
Source /usr/share/git/completion/git-prompt.sh
for your shell:
~/.bashrc
source /usr/share/git/completion/git-prompt.sh
and use __git_ps1
inside PS1
or PROMPT_COMMAND
. See Don't Reinvent the Wheel for details.
/etc/bash.bashrc
file can be found here[dead link 2014-08-05]. See also the gentoo-bashrcAUR package from AUR.