|

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:

ShellConfig 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:

CodeMeaning
\dCurrent date
\HFull hostname
\hHostname (short)
\nNew line character
\rCarriage return
\tTime in 24-hour format
\@Time in 12-hour format with AM/PM
\uUsername
\wWorking directory (full path)
\WLast 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:

CodeColor
\033[30mBlack
\033[30mDark gray
\033[31mRed
\033[31mLight red
\033[32mGreen
\033[32mLight green
\033[33mBrown
\033[33mYellow
\033[34mBlue
\033[34mLight blue
\033[35mPurple
\033[35mLight purple
\033[36mCyan
\033[36mLight cyan
\033[37mLight gray
\033[37mWhite

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:

CodeColor
\033[40mBlack
\033[41mRed
\033[42mGreen
\033[43mBrown
\033[44mBlue
\033[45mPurple
\033[46mCyan
\033[47mLight gray

Example — purple text on white background, then back to default:

PS1="\[\033[47;35m\]< \u - \W > \033[0m "

List of text format codes:

CodeEffect
\033[0mReset formatting to default
\033[1mBold text
\033[4mUnderline text
\033[7mInverse

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

CodeMeaning
\uUsername
\hHostname
\HFull hostname
\wFull working directory
\WLast part of working directory
\dCurrent date
\t24-hour time
\@12-hour time with AM/PM
\!History ID
\nNew line
\[ \]Wrap non-printing sequences

Colors

CodeColor
\033[30mBlack
\033[31mRed
\033[32mGreen
\033[33mYellow / Brown
\033[34mBlue
\033[35mPurple
\033[36mCyan
\033[37mWhite / Light gray

Backgrounds

CodeColor
\033[40mBlack
\033[41mRed
\033[42mGreen
\033[43mBrown
\033[44mBlue
\033[45mPurple
\033[46mCyan
\033[47mLight gray

Text Formats

CodeEffect
\033[0mReset to default
\033[1mBold
\033[4mUnderline
\033[7mInverse

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

PitfallProblemSolution
Missing \[ \]Long lines wrap incorrectlyWrap non-printing codes
No color resetText stays coloredAdd \033[0m
Wrong config fileChanges ignoredUse ~/.bashrc for Bash
Forgot to sourceChanges not appliedsource ~/.bashrc
Too much infoCluttered promptKeep it short and useful
Special chars unescapedBroken promptQuote 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 / CodePurposeExample
PS1Main shell promptPS1="\u > "
\uUsernamekronos
\h / \HHostnameolympos
\w / \WWorking directory~/projects
\t / \@Time14:32
\dDateMon Jan 15
\[ \]Non-printing wrapperaround color codes
\033[0mReset formattingend of prompt
\033[1mBoldtext
\033[4mUnderlinetext
\033[7mInversetext
\033[35mPurple textforeground
\033[47mWhite backgroundbackground

Key takeaways:

  • The shell prompt is controlled by PS1
  • Temporary: set PS1 directly in the terminal
  • Permanent: add to ~/.bashrc (Bash), ~/.zshrc (Zsh), or ~/.config/fish/config.fish (Fish), then source it
  • Use escape codes like \u, \h, \w, \t, \d for 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!