Customizing PS1
Colorize and customize terminal prompt
Section titled “Colorize and customize terminal prompt”This is how the author sets their personal PS1 variable:
gitPS1(){ gitps1=$(git branch 2>/dev/null | grep '*') gitps1="${gitps1:+ (${gitps1/#\* /})}" echo "$gitps1"}#Please use the below function if you are a mac usergitPS1ForMac(){ git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'}timeNow(){ echo "$(date +%r)"}if [ "$color_prompt" = yes ]; then if [ x$EUID = x0 ]; then PS1='\[\033[1;38m\][$(timeNow)]\[\033[00m\] \[\033[1;31m\]\u\[\033[00m\]\[\033[1;37m\]@\[\033[00m\]\[\033[1;33m\]\h\[\033[00m\] \[\033[1;34m\]\w\[\033[00m\]\[\033[1;36m\]$(gitPS1)\[\033[00m\] \[\033[1;31m\]:/#\[\033[00m\] ' else PS1='\[\033[1;38m\][$(timeNow)]\[\033[00m\] \[\033[1;32m\]\u\[\033[00m\]\[\033[1;37m\]@\[\033[00m\]\[\033[1;33m\]\h\[\033[00m\] \[\033[1;34m\]\w\[\033[00m\]\[\033[1;36m\]$(gitPS1)\[\033[00m\] \[\033[1;32m\]:/$\[\033[00m\] ' fielse PS1='[$(timeNow)] \u@\h \w$(gitPS1) :/$ 'fiAnd this is how my prompt looks like:

Color reference:
# Colorstxtblk='\e[0;30m' # Black - Regulartxtred='\e[0;31m' # Redtxtgrn='\e[0;32m' # Greentxtylw='\e[0;33m' # Yellowtxtblu='\e[0;34m' # Bluetxtpur='\e[0;35m' # Purpletxtcyn='\e[0;36m' # Cyantxtwht='\e[0;37m' # Whitebldblk='\e[1;30m' # Black - Boldbldred='\e[1;31m' # Redbldgrn='\e[1;32m' # Greenbldylw='\e[1;33m' # Yellowbldblu='\e[1;34m' # Bluebldpur='\e[1;35m' # Purplebldcyn='\e[1;36m' # Cyanbldwht='\e[1;37m' # Whiteunkblk='\e[4;30m' # Black - Underlineundred='\e[4;31m' # Redundgrn='\e[4;32m' # Greenundylw='\e[4;33m' # Yellowundblu='\e[4;34m' # Blueundpur='\e[4;35m' # Purpleundcyn='\e[4;36m' # Cyanundwht='\e[4;37m' # Whitebakblk='\e[40m' # Black - Backgroundbakred='\e[41m' # Redbadgrn='\e[42m' # Greenbakylw='\e[43m' # Yellowbakblu='\e[44m' # Bluebakpur='\e[45m' # Purplebakcyn='\e[46m' # Cyanbakwht='\e[47m' # Whitetxtrst='\e[0m' # Text ResetNotes:
Show git branch name in terminal prompt
Section titled “Show git branch name in terminal prompt”You can have functions in the PS1 variable, just make sure to single quote it or use escape for special chars:
gitPS1(){ gitps1=$(git branch 2>/dev/null | grep '*') gitps1="${gitps1:+ (${gitps1/#\* /})}" echo "$gitps1"}
PS1='\u@\h:\w$(gitPS1)$ 'It will give you a prompt like this:
user@Host:/path (master)$Notes:
- Make the changes in
~/.bashrcor/etc/bashrcor~/.bash_profileor~./profilefile (depending on the OS) and save it. - Run
source ~/.bashrc(distro specific) after saving the file.
Change PS1 prompt
Section titled “Change PS1 prompt”To change PS1, you just have to change the value of PS1 shell variable. The value can be set in ~/.bashrc or /etc/bashrc file, depending on the distro. PS1 can be changed to any plain text like:
PS1="hello "Besides the plain text, a number of backslash-escaped special characters are supported:
| Format | Action |
|---|---|
\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 |
So for example, we can set PS1 to:
PS1="\u@\h:\w\$ "And it will output:
user@machine:~$
Show a git branch using PROMPT_COMMAND
Section titled “Show a git branch using PROMPT_COMMAND”If you are inside a folder of a git repository it might be nice to show the current branch you are on. In ~/.bashrc or /etc/bashrc add the following (git is required for this to work):
function prompt_command { # Check if we are inside a git repository if git status > /dev/null 2>&1; then # Only get the name of the branch export GIT_STATUS=$(git status | grep 'On branch' | cut -b 10-) else export GIT_STATUS="" fi}# This function gets called every time PS1 is shownPROMPT_COMMAND=prompt_command
PS1="\$GIT_STATUS \u@\h:\w\$ "If we are in a folder inside a git repository this will output:
branch user@machine:~$
And if we are inside a normal folder:
user@machine:~$
Show time in terminal prompt
Section titled “Show time in terminal prompt”timeNow(){ echo "$(date +%r)"}PS1='[$(timeNow)] \u@\h:\w$ 'It will give you a prompt like this:
[05:34:37 PM] user@Host:/path$Notes:
- Make the changes in
~/.bashrcor/etc/bashrcor~/.bash_profileor~./profilefile (depending on the OS) and save it. - Run
source ~/.bashrc(distro specific) after saving the file.
Show previous command return status and time
Section titled “Show previous command return status and time”Sometimes we need a visual hint to indicate the return status of previous command. The following snippet make put it at the head of the PS1.
Note that the __stat() function should be called every time a new PS1 is generated, or else it would stick to the return status of last command of your .bashrc or .bash_profile.
# -ANSI-COLOR-CODES- #Color_Off="\033[0m"###-Regular-###Red="\033[0;31m"Green="\033[0;32m"Yellow="\033[0;33m"####-Bold-####
function __stat() { if [ $? -eq 0 ]; then echo -en "$Green ✔ $Color_Off " else echo -en "$Red ✘ $Color_Off " fi}
PS1='$(__stat)'PS1+="[\t] "PS1+="\e[0;33m\u@\h\e[0m:\e[1;34m\w\e[0m \n$ "
export PS1
