Linux CLI 25 🐧 Prompt Customization
PS1="<\u - \W > "
PS1="\[\033[35m< \u - \W > \033[37m\] "
PS1="\[\033[7;35m< \u - \W > \033[0;37m\] "
PS1="\[\033[47;35m\]< \u - \W > \033[0m "
PS1="\[\033[47;4;35m\]< \u - \W > \033[0m "
a – prompt customization intro
You customize the CLI prompt by editing the PS1 variable.
Permanent changes — edit the config file for your shell:
| Shell | Config File |
|---|---|
| Bash | ~/.bashrc |
| Zsh | ~/.zshrc |
| Fish | ~/.config/fish/config.fish |
Example — set a permanent prompt:
PS1="TestPrompt > "
export PS1
Apply the changes:
source ~/.bashrc
Temporary testing — just set PS1 directly in the terminal:
PS1="\u >" # shows only the username
b – prompt escape codes
Common escape codes for building shell prompts:
| Code | Meaning |
|---|---|
\d | Current date |
\H | Full hostname |
\h | Hostname (short) |
\n | New line character |
\r | Carriage return |
\t | Time in 24-hour format |
\@ | Time in 12-hour format with AM/PM |
\u | Username |
\w | Working directory (full path) |
\W | Last part of the working directory |
\! | History ID of the command |
\[ | Start of non-printing character sequence |
\] | End of non-printing character sequence |
Example — shows <username - working directory last>:
PS1="<\u - \W > "
c – prompt colors codes
List of foreground color codes:
| Code | Color |
|---|---|
\033[30m | Black |
\033[30m | Dark gray |
\033[31m | Red |
\033[31m | Light red |
\033[32m | Green |
\033[32m | Light green |
\033[33m | Brown |
\033[33m | Yellow |
\033[34m | Blue |
\033[34m | Light blue |
\033[35m | Purple |
\033[35m | Light purple |
\033[36m | Cyan |
\033[36m | Light cyan |
\033[37m | Light gray |
\033[37m | White |
Example — shows <username - working directory last> in purple, then back to white:
PS1="\[\033[35m< \u - \W > \033[37m\] "
d – prompt background colors and text format codes
List of background color codes:
| Code | Color |
|---|---|
\033[40m | Black |
\033[41m | Red |
\033[42m | Green |
\033[43m | Brown |
\033[44m | Blue |
\033[45m | Purple |
\033[46m | Cyan |
\033[47m | Light gray |
Example — purple text on white background, then back to default:
PS1="\[\033[47;35m\]< \u - \W > \033[0m "
List of text format codes:
| Code | Effect |
|---|---|
\033[0m | Reset formatting to default |
\033[1m | Bold text |
\033[4m | Underline text |
\033[7m | Inverse |
Example — purple text, white background, underlined, then back to default:
PS1="\[\033[47;4;35m\]< \u - \W > \033[0m "
Complete Example Session
# ============================================
# PART 1: VIEW CURRENT PROMPT
# ============================================
$ echo $PS1
\[\033[35m\]\u@\h:\w\$\033[0m
# ============================================
# PART 2: TEMPORARY PROMPT CHANGES
# ============================================
$ PS1="\u > "
kronos >
$ PS1="<\u - \W > "
<kronos - projects >
$ PS1="\[\033[35m< \u - \W > \033[37m\] "
<kronos - projects > # shown in purple, then white
$ PS1="\[\033[47;35m\]< \u - \W > \033[0m "
<kronos - projects > # purple text, white background, back to default
$ PS1="\[\033[47;4;35m\]< \u - \W > \033[0m "
<kronos - projects > # purple, white bg, underlined, back to default
# ============================================
# PART 3: ADD TIME AND DATE
# ============================================
$ PS1="[\t] \u@\h:\w\$ "
[14:32:05] kronos@olympos:~$
$ PS1="[\d \@] \u > "
[Mon Jan 15 02:32 PM] kronos >
# ============================================
# PART 4: MAKE PERMANENT IN ~/.bashrc
# ============================================
$ nano ~/.bashrc
# Add at the bottom:
# PS1="\[\033[35m< \u - \W > \033[37m\] "
$ source ~/.bashrc
<kronos - projects >
# ============================================
# PART 5: VERIFY PERSISTENCE
# ============================================
$ bash # open new shell
<kronos - projects > # ✅ still there
$ exit
Quick Reference
Escape Codes
| Code | Meaning |
|---|---|
\u | Username |
\h | Hostname |
\H | Full hostname |
\w | Full working directory |
\W | Last part of working directory |
\d | Current date |
\t | 24-hour time |
\@ | 12-hour time with AM/PM |
\! | History ID |
\n | New line |
\[ \] | Wrap non-printing sequences |
Colors
| Code | Color |
|---|---|
\033[30m | Black |
\033[31m | Red |
\033[32m | Green |
\033[33m | Yellow / Brown |
\033[34m | Blue |
\033[35m | Purple |
\033[36m | Cyan |
\033[37m | White / Light gray |
Backgrounds
| Code | Color |
|---|---|
\033[40m | Black |
\033[41m | Red |
\033[42m | Green |
\033[43m | Brown |
\033[44m | Blue |
\033[45m | Purple |
\033[46m | Cyan |
\033[47m | Light gray |
Text Formats
| Code | Effect |
|---|---|
\033[0m | Reset to default |
\033[1m | Bold |
\033[4m | Underline |
\033[7m | Inverse |
Best Practices
✅ Do This:
# Always wrap non-printing sequences in \[ \]
PS1="\[\033[35m\]\u@\h\[\033[0m\] \$ " # ✅
# Reset colors after each colored segment
PS1="\[\033[35m\]< \u - \W > \[\033[0m\]" # ✅
# Test temporarily before making permanent
PS1="\u > " # ✅ quick test
# then edit ~/.bashrc
# Source after editing
nano ~/.bashrc
source ~/.bashrc # ✅
# Keep the prompt informative but short
PS1="\[\033[35m\]\u@\h:\W\$ \[\033[0m\]" # ✅
❌ Don’t Do This:
# Forget \[ \] around color codes — breaks line wrapping
PS1="\033[35m\u@\h\033[0m \$ " # ❌
# Leave colors active — prompt stays colored
PS1="\[\033[35m\]\u@\h \$ " # ❌ no reset
# Overwrite .bashrc instead of appending
echo 'PS1="\u >"' > ~/.bashrc # ❌ wipes config
# Forget to source
nano ~/.bashrc # ❌ changes not applied
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
Missing \[ \] | Long lines wrap incorrectly | Wrap non-printing codes |
| No color reset | Text stays colored | Add \033[0m |
| Wrong config file | Changes ignored | Use ~/.bashrc for Bash |
| Forgot to source | Changes not applied | source ~/.bashrc |
| Too much info | Cluttered prompt | Keep it short and useful |
| Special chars unescaped | Broken prompt | Quote PS1 properly |
Real-World Examples
1. Minimal Clean Prompt
PS1="\[\033[32m\]\u@\h\[\033[0m\]:\[\033[34m\]\w\[\033[0m\]\$ "
# kronos@olympos:~$
2. Two-Line Prompt with Time
PS1="\[\033[35m\][\t] \u@\h\[\033[0m\]\n\[\033[32m\]\w\[\033[0m\]\$ "
# [14:32:05] kronos@olympos
# ~$
3. Minimal Username + Directory
PS1="<\u - \W > "
# <kronos - projects >
4. Purple on White Background
PS1="\[\033[47;35m\]< \u - \W > \033[0m "
# <kronos - projects > (purple text, white bg)
5. Underlined Purple on White
PS1="\[\033[47;4;35m\]< \u - \W > \033[0m "
# <kronos - projects > (purple, white bg, underlined)
6. Show Git Branch (Dynamic Prompt)
# In ~/.bashrc
parse_git_branch() {
git branch 2>/dev/null | grep '*' | sed 's/* //'
}
PS1="\[\033[35m\]\u@\h\[\033[0m\]:\[\033[34m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[0m\]\$ "
7. Permanent Change
$ echo 'PS1="\[\033[35m\]< \u - \W > \[\033[37m\]"' >> ~/.bashrc
$ source ~/.bashrc
<kronos - projects >
Summary
| Variable / Code | Purpose | Example |
|---|---|---|
PS1 | Main shell prompt | PS1="\u > " |
\u | Username | kronos |
\h / \H | Hostname | olympos |
\w / \W | Working directory | ~/projects |
\t / \@ | Time | 14:32 |
\d | Date | Mon Jan 15 |
\[ \] | Non-printing wrapper | around color codes |
\033[0m | Reset formatting | end of prompt |
\033[1m | Bold | text |
\033[4m | Underline | text |
\033[7m | Inverse | text |
\033[35m | Purple text | foreground |
\033[47m | White background | background |
Key takeaways:
- The shell prompt is controlled by
PS1 - Temporary: set
PS1directly in the terminal - Permanent: add to
~/.bashrc(Bash),~/.zshrc(Zsh), or~/.config/fish/config.fish(Fish), thensourceit - Use escape codes like
\u,\h,\w,\t,\dfor dynamic info - Use color codes (
\033[3Xm), background codes (\033[4Xm), and format codes (\033[1m,\033[4m,\033[7m) - Always wrap non-printing sequences in
\[ \]and reset with\033[0m - Never overwrite your config file — append to it
- Keep prompts short, informative, and readable
Remember: PS1 is your prompt. Test changes temporarily first, then make them permanent in your shell’s config file. Use \[ \] around every color/format code so line wrapping works correctly, and always reset with \033[0m. A good prompt tells you who you are, where you are, and optionally when — without getting in the way.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!