Bash prompt

This article describes how to set the shell prompt within the bash shell, and offers several alternatives.

Briefly, the environment variable PS1 stores the string that is printed as the prompt, and the variable PROMPT_COMMAND stores an optional command that is executed immediately before the prompt is displayed.

PS1
The PS1 variable stores the command-line prompt that bash prints. Various backslash-codes within the PS1 string are evaluated each time the prompt is printed, allowing it to include information about the current environment, directory, username, etc.

Simple prompts
export PS1='\u@\h:\w\$ '        # Single quotes with single backslashes results in: matt@nyarlathotep:/var/home/matt$

Colourful prompts
For a colourful bash prompt that looks like: username@hostname/ pwd $  use the following: export PS1='\[\e[32m\]\u@\h/\[\e[1;31m\]\w\[\e[1;34m\]\$\[\e[0m\] '

Multi-line prompts
Prompts may also span multiple lines, which is useful when they incorporate the current directory name (which might be very large). export PS1='\[\033[01;36m\]\w\[\033[00m\]\n\[\033[01;32m\]\u@\h\[\033[00m\]\$ ' results in something like: /usr/local/apache2/build/httpd-2.2.4/build  matt@nyarlathotep $ 

In this example, the very long directory name is on a line by itself - for ease of cut-and-paste - and the username and hostname follow, ending with the traditional $ for a non-root user or # for root.

Advanced prompts
Here is 3-line prompt function from askapache with 16 and 256 color support that is optimized by utilizing builtin bash commands. Just add and call this function from your startup file.

function aa_prompt_defaults {  local C="`tput setaf 33`" colors=`tput colors 2>/dev/null`;

if $colors -eq 256 ; then AA_P='mf=x mt=x n=0; while $n < 1 ;do read a mt a; read a mf a; (( n++ )); done/dev/null`}

PS1="\[\e[m\n\e[1;30m\][\$\$:\$PPID \j:\!\[\e[1;30m\]]\[\e[0;36m\] \T \d \[\e[1;30m\][${C}\u@\H\[\e[1;30m\]:\[\e[0;37m\]${SSH_TTY/\/dev\/} \[\e[0;32m\]+${SHLVL}\[\e[1;30m\]] \[\e[1;37m\]\w\[\e[0;37m\]\n\\$ "

export PS1 AA_P PROMPT_COMMAND SSH_TTY }

results in something like:

3-lines:
 * 1) The current memory used/memory available.
 * 2) (PID of shell):(PID of shell parent) (# of jobs) (history #) (time and date) (username)@(hostname): (SSH_TTY or tty) (+Shell Level) (CWD)
 * 3) If the effective UID is 0, a #, otherwise a $

This PROMPT_COMMAND turns on terminal echo, saves history, turns on cursor, and then grabs the memory used/avail when $SECONDS is an even number for display by PS1.

PS1 syntax
When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:

PROMPT_COMMAND
In addition, the PROMPT_COMMAND variable is run every time the shell prompt is printed. Many use this to change the xterm title bar:

export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'

Make it permanent
If you restart your computer, you still want the same login prompt. That means, you must set $PS1 permanentely. You must set it specifically for the bash as well as other shells like zsh, csh and ksh. You have to set it for logIn-shells, and for non-login-shells. You can set it for all users or for a specific user. Here are the files you will need to set it in: