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:
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 $
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 $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 $
New line, username@hostname, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access