How To's

Customizing the bash prompt

8,125 views · 2 found this helpful

Was this helpful?

The following is a basic tutorial covering how to customize your bash prompt.

Before we begin, the environment variable that we will be editing is PS1. We will be placing this variable in the ~/.bashrc file. To see the default prompt settings, enter:

echo $PS1
[\u@\h \W]\$

Before we continue, let's break down some of the more useful escape characters that you can use in your new bash prompt.

  • \d -> the date in "Weekday Month Date" format (e.g., "Tue May 26")
  • \D{format} -> the format is passed to strftime (for more information, enter man 3 strftime) and the result is inserted into the prompt string. Here is a list of a few of the available options for format (e.g., \D{%F} -> YYYY-MM-DD):
    • %D -> Month/Day/Year (American format)
    • %F -> Year/Month/Day
    • %H -> The hour as a decimal number using a 24-hour clock (range 00 to 23)
    • %I -> The hour as a decimal number using a 12-hour clock (range 01 to 12)
    • %M -> The minute as a decmal number (range 00 to 59)
    • %R -> The time in 24-hour notation (hour:minute)
    • empty -> locale-specific time representation
  • \h -> the hostname up to the first '.'
  • \H -> the hostname
  • \@ -> the current time in 12-hour am/pm format
  • \u -> the username of the current user
  • \w -> the current working directory, with $HOME abbreviated with a tilde
  • \W -> the basename of the current working directory, with $HOME abbreviated with a tilde
  • \! -> the histor number of this command
  • \$ -> if the effective UID is 0, a *, otherwise a $

Obviously, this isn't all the available options, but if you are interested, enter man -P 'less -p "an ASCII bell character "' bash

Now, let's write our own simple version:

nano ~/.bashrc

and add the following

# Our custom bash prompt
PS1='{\@}|\u@\h \w|\$'

source ~/.bashrc

Now your command prompt will look something like this:

{01:45 PM}|root@localhost ~|#

Congratulations! You have now customized your bash prompt. Later, we'll go into more advanced techniques (like adding colour).

  • Updated

← All articles

Still stuck?

Our engineers answer tickets directly, 24/7.