How to Personalize Your Bash Prompt: Add Info, Colors, and Multi‑Line Layouts
This guide explains how to customize the Bash prompt by editing the PS1/PS2 variables in .bashrc, using special escape sequences for user, host, directory, date, and colors, and creating multi‑line prompts with clear examples and explanations.
When you open a Linux shell you see a default Bash prompt like
[user@host ~]$. This article shows how to customize that prompt to display useful information, colors, and multiple lines.
How to Set the Bash Prompt
The prompt is defined by the environment variable
PS1. For multi‑line prompts,
PS2is used.
<code>export PS1="[Linux Rulez]$ "
export PS2="... "
if true; then
... echo "Success!"
... fi
Success!</code>Where to Define PS1
The default value is set in
/etc/bashrc. To customize, add your settings to
~/.bashrcinstead of modifying the system file.
Common Escape Sequences
\u: username
\h: short hostname
\W: current directory basename ("~" for home)
\s: shell name (bash or sh)
\v: shell version
Additional Useful Sequences
\d: date (e.g., "Tue Jun 27")
\D{fmt}: custom date format (see
strftime)
\D{%c}: localized date and time
\n: newline (for multi‑line prompts)
\w: full current working directory path
\H: full hostname
Multi‑Line Prompt Example
Use
\nto split the prompt into two lines, showing date, time, and directory on the first line and user/host on the second.
<code>PS1="\D{%c} \w\n[\u@\H]$ "</code>Adding Colors
Colors can be added with ANSI escape codes. Below is an example that colors the date red, the directory cyan, and the username with a yellow background.
For more color codes, refer to the "Bash prompt HOWTO" documentation.
What does your favorite custom prompt look like? Share your creations in the comments.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.