Customize Your Bash Prompt: Colors, Variables, and Multi‑Line Layouts
Learn how to personalize the Bash command‑line prompt by setting the PS1 variable, using special escape sequences for user, host, directory, date, and colors, and creating multi‑line prompts, all with clear examples and safe configuration steps.
How to Set the Bash Prompt
Bash displays a prompt defined by the PS1 environment variable; for multi‑line prompts, PS2 is used. You can change these variables directly in the shell or, more permanently, by adding export statements to your ~/.bashrc file.
export PS1="[Linux Rulez]$"
export PS2="..."Where the Default PS1 Value Comes From
The system’s default prompt is set in /etc/bashrc. A typical default assignment looks like this:
["$PS1" = "\s-\v\$"] && PS1="[\u@\h \W]\$"If the current PS1 matches the default pattern, Bash replaces it with [\u@\h \W]\$. To avoid overwriting system files, place your custom export PS1=… lines in ~/.bashrc instead of editing /etc/bashrc.
Common Escape Sequences in PS1
\u– username \h – short hostname \W – basename of the current working directory ("~" for home) \s – name of the shell (e.g., bash) \v – shell version
Additional Useful Escape Sequences
\d– date in "Tue Jun 27" format \D{fmt} – custom date format (see man strftime) \D{%c} – locale‑aware date and time \n – newline (for multi‑line prompts) \w – full current working directory path \H – full hostname
Creating a Multi‑Line Prompt
When the prompt becomes long, you can split it onto two lines using \n. The following example shows the first line with date, time, and directory, and the second line with username and hostname:
PS1="\D{%c} \w
[\u@\H]$ "Adding Colors to the Prompt
Colors are added with ANSI escape sequences wrapped in \[ ... \]. Below is an example that makes the date red, the directory cyan, and the username appear with a yellow background:
PS1="\[\e[31m\]\D{%c}\[\e[0m\] \[\e[36m\]\w\[\e[0m\]
[\[\e[1;43m\]\u\[\e[0m\]@\H]$ "Resulting prompts may look like the following screenshots:
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
