Bash prompt

From LQWiki
Jump to navigation Jump to search

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</proc/meminfo; export AA_PP="\033[38;5;2m"$((mf/1024))/"\033[38;5;89m"$((mt/1024))MB';
   else
      AA_P='mf=x mt=x n=0; while [[ $n < 1 ]];do read a mt a; read a mf a; (( n++ )); done</proc/meminfo; export AA_PP="\033[92m"$((mf/1024))/"\033[32m"$((mt/1024))MB';
   fi;

   eval $AA_P;

   PROMPT_COMMAND='stty echo;history -a; echo -en "\e[34h\e[?25h"; (($SECONDS % 2==0 )) && eval $AA_P; echo -en "$AA_PP"';

   SSH_TTY=${SSH_TTY:-`tty 2>/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:

370/15499MB
[2620:2910 0:21] 10:50:50 Wed Apr 02 [crown@royal:pts/2 +4] ~/.thunderbird
#


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:

Characters Decoded as
\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
\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 (uses the value of the PROMPT_DIRTRIM variable)
\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 (root), a #, otherwise a $
\\ 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
\nnn the character corresponding to the octal number nnn
\D{format} the format is passed to strftime and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required

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:

for login-shells for non-login-shells
for all users /etc/profile /etc/bashrc
for a specific user ~/.bash_profile ~/.bashrc

See also