Customizing Your Prompt

Problem

The default bash prompt is usually something uninformative that ends with $ and doesn’t tell you much, so you would like to customize it to show information you find useful.

Solution

Customize the $PS1 and $PS2 variables as you desire.

The default prompt varies depending on your system. bash itself will show its major and minor version (\s-\v\$), for example, bash-3.00$. However, your operating system may have its own default, such as [user@host~]$ ([\u@\h\W]\$) for Fedora Core 5. This solution presents eight basic prompts and three fancier prompts.

Basic prompts

Here are eight examples of more useful prompts that will work with bash-1.14.7 or newer. The trailing \$ displays # if the effective UID is zero (i.e., you are root) and $ otherwise:

  1. Username@hostname, the date and time, and the current working directory:

    $ export PS1='[\u@\h \d \A] \w \$ '
    [jp@freebsd Wed Dec 28 19:32] ~ $ cd /usr/local/bin/
    [jp@freebsd Wed Dec 28 19:32] /usr/local/bin $
  2. Username@long-hostname, the date and time in ISO 8601 format, and the base-name of the current working directory (\W):

    $ export PS1='[\u@\H \D{%Y-%m-%d %H:%M:%S%z}] \W \$ '
    [jp@freebsd.jpsdomain.org 2005-12-28 19:33:03-0500] ~ $ cd /usr/local/bin/
    [jp@freebsd.jpsdomain.org 2005-12-28 19:33:06-0500] bin $
  3. Username@hostname, bash version, and the current working directory (\w):

    $ export PS1='[\u@\h \V \w] \$ '
    [jp@freebsd 3.00.16] ~ $ cd /usr/local/bin/
    [jp@freebsd 3.00.16] /usr/local/bin $
  4. New line, username@hostname, ...

Get bash Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.