Tag Archives: bash

How to change the color and format of your bash prompt

I was recently asked how to change the color on the bash prompt so I put together this article

Here is your bash prompt in all its glory

root@server1 [~]#

# is for root access
$ is for user access

Display the current environment variable (PS1)

root@server1 [~]# echo $PS1
\u@\h [\w]#

PS1 is our bash shell environment variable

By default the command prompt above is set to \u@\h [\W]# This will vary on different distros of Linux

\u : Display the username
\h : Display the hostname
[\W] : Print the current working directory in [ ]’s

Variable list
\a The ASCII bell character (you can also type \007)
\d Date in “Wed Sep 06” format \e ASCII escape character (you can also type \033)
\h First part of hostname (such as “mybox”)
\H Full hostname (such as “mybox.mydomain.com”)
\l The name of the shell’s terminal device (such as “ttyp4”)
\j The number of processes you’ve suspended in this shell by hitting ^Z
\n Newline \r Carriage return \s The name of the shell executable (such as “bash”)
\t Time in 24-hour format (such as “23:01:01”)
\T Time in 12-hour format (such as “11:01:01”)
\@ Time in 12-hour format with am/pm
\u Your username
\v Version of bash (such as 2.04)
\V Bash version, including patchlevel
\w Current working directory (such as “/home/user”)
\W The “basename” of the current working directory (such as “mkahn”)
\! Current command’s position in the history buffer
\# Command number (this will count up at each prompt, as long as you type something)
\\$ If you are not root, inserts a “$”; if you are root, you get a “#”
\xxx Inserts an ASCII character based on three-digit number xxx (replace unused digits with zeros, such as “\007”)
\\ A backslash
\[ This sequence should appear before a sequence of characters that don’t move the cursor (like color escape sequences). This allows bash to calculate word wrapping correctly.
\] This sequence should appear after a sequence of non-printing characters.

Add colors to the prompt
Colors are selected by adding special sequences to PS1 — basically sandwiching numeric values between a “\e[” (escape open-bracket) and an “m”. If we specify more than one numeric code, we separate each code with a semicolon.
\e0;31m – red non bold
\e1;31m – red bold

\e[0m – reset the colors to default values (black and white)

Color codes
Black 0;30 Dark Gray 1;30
Blue 0;34 Light Blue 1;34
Green 0;32 Light Green 1;32
Cyan 0;36 Light Cyan 1;36
Red 0;31 Light Red 1;31
Purple 0;35 Light Purple 1;35
Brown 0;33 Yellow 1;33
Light Gray 0;37 White 1;37
Replace digit 0 with a 1 to get a bold version

Replace 0 digit with 40 (black), 41 (red), 42, (green), 43 (yellow), 44 (blue), 45 (purple), 46 (cyan), 47 (light gray) background color (highlight).

Red color prompt
export PS1="\e[0;31m[\u@\h \w]\\$ \e[0m"

Green color prompt
export PS1="\e[0;32m[\u@\h \w]\\$ \e[0m"

Modify the current bash prompt to display user@full hostname and working directory with time

Setup a new shell prompt:
root@server1 [~]# export PS1="\u@\H [\w] \T \$ "
root@server.mybox.com [/home/myfolder] 01:07:57 $

PS1 : Current bash prompt
\u : Display the username
@ : @ sign after username
\H : Display the full hostname
[\w] : Display the current working directory in [ ]’s
\T : Display the time in 12 hr format
\$ : display hash

Making it stick

The command ran before were just for that session, when you login it will be lost. If you would like to make this BASH shell modification permanent add the export command to your .bash_profile or .bashrc file

vi .bashrc

.bash_profile (osx) and .bashrc (Linux)
use pico or nano if you do not know how to use vi

Add export line before fi
export PS1="\e[0;31m[\u@\h \w]\\$ \e[0m"

Save and close the file
Now you have a cool modified bash shell prompt


xmas bash prompt1
export PS1="\e[1;32m[\u@\h \e[1;31m\\w\e[1;32m]\$ \e[m"

xmas bash prompt 2
export PS1="\e[42;31m[\u@\h \w]\$ \e[m"


hanukkah bash prompt
export PS1="\e[0;34m[\u\e[0;34m@\h[\e[0;33m\w\e[0m\e[0m\e[0;34m]#\e[0m "


the matrix
export PS1="\e[0;32m\u@\h [\w]# "


export PS1="\e[0;32m\u\e[0m\e[0;36m@\e[0m\e[0;34m\h \e[0m\e[0;36m\w \e[0m# "


sources: http://www.pantz.org/software/shell/enhancingshellprompt.html