Customize Your Bash Prompt: Add Time, Colors, and Permanent Settings
This guide explains how to view and modify the Bash prompt using the PS1 variable, add time stamps, apply ANSI color codes, and make the changes permanent by editing .bashrc or global configuration files, with step‑by‑step commands and examples.
Linux terminals display a command prompt that can be customized through the PS1 shell variable. Bash reads PS1 to show the first prompt before each command.
Viewing the current prompt
echo $PS1
[\u@\h \W]\$The output shows escape sequences that Bash expands when displaying the prompt.
Understanding PS1 escape sequences
\u – current username
\h – hostname (short)
\W – current working directory (basename)
$ – displays # for root, otherwise
$Adding time to the prompt
To include the 24‑hour time, append the following command to ~/.bashrc and reload the file:
echo 'export PS1="[\\u@\\h \\W \\A]\\$ "' >> ~/.bashrc
source ~/.bashrcAfter reloading, the prompt shows the current time as shown in the image below.
Making changes permanent
For a user‑specific permanent change, place the export PS1=… line in ~/.bashrc. To apply it system‑wide for all users, edit /etc/bash.bashrc or /etc/bashrc.
Adding colors to the prompt
ANSI escape codes can colorize the prompt. For example, a red prompt is set with: export PS1="\e[0;31m[\\u@\\h \\W]\\$ \e[m " Common color codes are:
'\e[0;30m' # black
'\e[0;31m' # red
'\e[0;32m' # green
'\e[0;33m' # yellow
'\e[0;34m' # blue
'\e[0;35m' # magenta
'\e[0;36m' # cyan
'\e[0;37m' # white
'\e[1;30m' # bright black (bold)
'\e[1;31m' # bright red (bold)
'\e[1;32m' # bright green (bold)
'\e[1;33m' # bright yellow (bold)
'\e[1;34m' # bright blue (bold)
'\e[1;35m' # bright magenta (bold)
'\e[1;36m' # bright cyan (bold)
'\e[1;37m' # bright white (bold)
'\e[4;30m' # black underline
'\e[4;31m' # red underline
'\e[40m' # black background
'\e[41m' # red background
'\e[0m' # reset to defaultCustomizing each part with different colors
Each component of the prompt can have its own color. The following example colors the username red, the hostname magenta, the directory green, and the $ sign bold yellow:
export PS1="\e[31m[\\u\e[m@\e[35m\\h \e[32m\\W]\e[1;33m\\$\e[m "The color codes used are:
\e[31m – red for the username
\e[m – reset (used after the @ symbol)
\e[35m – magenta for the hostname
\e[32m – green for the current directory
\e[1;33m – bold yellow for the $ sign
By combining these sequences, you can create a Bash prompt that displays the information you need in colors that suit your preferences.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
