Enhance Your Bash Prompt

The command line is a powerful interface. A primary interface element is the prompt. Your systems provides you with a default prompt, but you can change it. Lets start by setting with a minimal prompt.

PS1=”\$ “

Bash has a whole set of characters, each representing a special value. A complete list of these are available in the bash man page. In the case of \$, it shows a $ for a regular user. When you become root, the \$ in your bash prompt is displayed as a #. While that works, you may want a little more information.

PS1=”\u@\h \$ “

the \u represents the current username, and the \h represents the hostname up to the first dot of the domain name. This is quite useful if you are connecting to different servers. Having your username and hostname in your prompt makes things easy.

Another useful piece of information is the date and time. There are 4 different ways to display the date and time.

\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

You may also be interested in knowing where you are in the file system, without having to do pwd each time. For that purpose, you can use \w or \W

You can also add color to your prompt. Below is an example using all the fields we’ve discussed.

PS1=”\n\n”
PS1+=”\W\n” # basedir
PS1+=”\u@\h\n” # username@host
PS1+=”\d \t \$ ” # Mon Dec 21 13:09:34

Color version of the same prompt

PS1=”\n\n”
PS1+=”\[\033[1;35m\]\W\e[m\n” # basedir
PS1+=”\[\033[1;37m\]\u@\h\e[m\n” # username@host
PS1+=”\[\033[1;36m\]\d \t \$\e[m ” # Mon Dec 21 13:09:34

Use these examples and the Bash man page to help you create your own prompt.

Sorry, but comments are closed. I hope you enjoyed the article