From LQWiki
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.
Contents |
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</nowiki>
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.
PS1 syntax
- \d Displays the date in "Weekday Month Date".
- \e ASCII escape (See the console_codes man page for what you can do).
- \h Displays the hostname (up to the first '.').
- \t Displays current time in 24-hour HH:MM:SS format.
- \u Displays the name of your user.
- \w Displays the name of the current directory.
- \W Displays only the basename of the current directory.
- \$ Displays '$' if you're not root, '#' if you are.
- \[ and \] Surround non-printing characters
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 |

This page is available under a