Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Last active June 11, 2020 18:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cellularmitosis/3686357 to your computer and use it in GitHub Desktop.
Save cellularmitosis/3686357 to your computer and use it in GitHub Desktop.
Source this in your bashrc for colored username and exit status

Blog 2012/9/9

index | next ->

Colorized Bash prompt

Source this in your ~/.bashrc for colorized username and exit status.

687474703a2f2f692e696d6775722e636f6d2f35323350442e706e67

Don't forget to copy this into root's bashrc as well!

# source this in your bashrc for colorized username and exit status.
# see https://gist.github.com/gists/3686357
# see http://i.imgur.com/523PD.png
# don't forget to copy this into root's bashrc as well
# --- colors for prompt
COLOR_RED="\[\e[31;40m\]"
COLOR_GREEN="\[\e[32;40m\]"
COLOR_YELLOW="\[\e[33;40m\]"
COLOR_BLUE="\[\e[34;40m\]"
COLOR_MAGENTA="\[\e[35;40m\]"
COLOR_CYAN="\[\e[36;40m\]"
COLOR_RED_BOLD="\[\e[31;1m\]"
COLOR_GREEN_BOLD="\[\e[32;1m\]"
COLOR_YELLOW_BOLD="\[\e[33;1m\]"
COLOR_BLUE_BOLD="\[\e[34;1m\]"
COLOR_MAGENTA_BOLD="\[\e[35;1m\]"
COLOR_CYAN_BOLD="\[\e[36;1m\]"
COLOR_NONE="\[\e[0m\]"
# --- prompt
promptFunc()
{
PREV_RET_VAL=$?;
PS1=""
# $USER seems unreliable. use whoami instead.
#if test "$USER" != "root"
if test "`whoami`" != "root"
then
PS1="${PS1}${COLOR_CYAN_BOLD}\u${COLOR_NONE}"
else
PS1="${PS1}${COLOR_RED_BOLD}\u${COLOR_NONE}"
fi
PS1="${PS1}@\h"
if test $PREV_RET_VAL -eq 0
then
PS1="${PS1}${COLOR_GREEN_BOLD}\\$ ${COLOR_NONE}"
else
PS1="${PS1}${COLOR_RED_BOLD}\\$ [${PREV_RET_VAL}] ${COLOR_NONE}"
fi
}
PROMPT_COMMAND=promptFunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment