Changing bash prompt

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search

← Using aliasesHomeSetting shell options →

  • Task: You need to customize your bash prompt by editing PS1 variable.
  • Display, your current prompt setting, enter:
echo $PS1

Sample outputs:

  • For testing purpose set PS1 as follows and notice the change:
PS1='your wish is my command : '

Sample outputs:

vivek@vivek-desktop:~$ PS1='your wish is my command : '
your wish is my command : 

Customizing Prompt

Bash shell allows prompt strings to be customized by inserting a number of backslash-escaped special characters. Quoting from the bash man page:

Sequence Description
\a An ASCII bell character (07)
\d The date in "Weekday Month Date" format (e.g., "Tue May 26")
\e An ASCII escape character (033)
\h The hostname up to the first .
\H The hostname (FQDN)
\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 T 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

You can use above backslash-escaped sequence to display name of the host with current working directory:

PS1='\h \W $ '

Adding color to prompt

It is quite easy to add colors to your prompt. Set green color prompt for normal user account[1]:

export PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '

And red color prompt for root user account:

export PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\] '

How do I make prompt setting permanent?

Edit your ~/.bashrc or ~/.bash_profile

vi ~/.bashrc

Append your PS1 definition:

export PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '

Save and close the file.


If PROMPT_COMMAND environment variable set, the value is executed as a command prior to issuing each primary prompt. In other words, the contents of this variable are executed as a regular Bash command just before Bash displays a prompt[2]:

PROMPT_COMMAND="echo Yahooo"

Sample outputs:

[vivek@vivek-desktop man]$ PROMPT_COMMAND="echo Yahooo"
[vivek@vivek-desktop man]$ date
Tue Oct 20 23:50:01 IST 2009

Creating complex prompt

Edit ~/.bashrc file:

vi ~/.bashrc

Add the following two shell functions[3]

bash_prompt_command() {
    # How many characters of the $PWD should be kept
    local pwdmaxlen=25
    # Indicate that there has been dir truncation
    local trunc_symbol=".."
    local dir=${PWD##*/}
    pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
    local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]

bash_prompt() {
    case $TERM in
         local TITLEBAR='\[\033]0;\u:${NEW_PWD}\007\]'
         local TITLEBAR=""
    local NONE="\[\033[0m\]"    # unsets color to term's fg color
    # regular colors
    local K="\[\033[0;30m\]"    # black
    local R="\[\033[0;31m\]"    # red
    local G="\[\033[0;32m\]"    # green
    local Y="\[\033[0;33m\]"    # yellow
    local B="\[\033[0;34m\]"    # blue
    local M="\[\033[0;35m\]"    # magenta
    local C="\[\033[0;36m\]"    # cyan
    local W="\[\033[0;37m\]"    # white
    # emphasized (bolded) colors
    local EMK="\[\033[1;30m\]"
    local EMR="\[\033[1;31m\]"
    local EMG="\[\033[1;32m\]"
    local EMY="\[\033[1;33m\]"
    local EMB="\[\033[1;34m\]"
    local EMM="\[\033[1;35m\]"
    local EMC="\[\033[1;36m\]"
    local EMW="\[\033[1;37m\]"
    # background colors
    local BGK="\[\033[40m\]"
    local BGR="\[\033[41m\]"
    local BGG="\[\033[42m\]"
    local BGY="\[\033[43m\]"
    local BGB="\[\033[44m\]"
    local BGM="\[\033[45m\]"
    local BGC="\[\033[46m\]"
    local BGW="\[\033[47m\]"
    local UC=$W                 # user's color
    [ $UID -eq "0" ] && UC=$R   # root's color
    PS1="$TITLEBAR ${EMK}[${UC}\u${EMK}@${UC}\h ${EMB}\${NEW_PWD}${EMK}]${UC}\\$ ${NONE}"
    # without colors: PS1="[\u@\h \${NEW_PWD}]\\$ "
    # extra backslash in front of \$ to make bash colorize the prompt
# init it by setting PROMPT_COMMAND
unset bash_prompt


  1. BASH Shell change the color of my shell prompt under Linux or UNIX
  2. PROMPT_COMMAND from Bash prompt howto
  3. Color bash prompt code taken from the official Arch Linux wiki

External links

← Using aliasesHomeSetting shell options →