Skip to main content

Prompt Customization (PS1) — Core Customization Technique in Bash


Learning Focus
  1. Understand what the Bash prompt (PS1) is and how it controls your command-line interface.
  2. Learn how to customize your PS1 variable to show username, hostname, path, time, and colors.
  3. Discover where and how PS1 customization is stored (user-level vs system-wide) — shown in tree view.
  4. Apply WordPress VPS–specific prompt styles (e.g., production vs staging color cues).
  5. Follow best practices for usability, safety, and shell performance.

1. What is PS1​

In Bash, PS1 (Prompt String 1) defines how your command-line prompt appears before each command you type. It’s an environment variable that can include dynamic information such as:

  • Username (\u)
  • Hostname (\h)
  • Current working directory (\w)
  • Shell symbol ($)
  • Colors, timestamps, and even emojis Default example:
echo $PS1

Typical output:

\u@\h:\w\$

Displayed as:

root@GC-SG-M16GB:/root#

2. Why Customize the Prompt​

ReasonDescription
ClarityIdentify which environment you’re in (root, user, staging, production).
SafetyAvoid destructive mistakes by labeling production servers visually.
UsabilityDisplay working directory, time, or Git branch for convenience.
ProfessionalismCreates a clean, structured workspace aligned with your workflow.
Example goal:
[PROD: root@wpstrategist:/var/www/html]#

3. Where Prompt Settings Are Stored​

ScopeFileApplies ToWhen Loaded
User-specific~/.bashrcCurrent userEvery new shell
System-wide/etc/bash.bashrcAll usersEvery new shell
Environment fallback/etc/profileLogin shellBefore .bashrc

4. File Locations (Tree View)​

User-Level Configuration

/home/
└── donnyariw/
├── .bashrc
├── .bash_profile
├── .bash_aliases
├── .bash_functions
└── .bash_prompt

System-Wide Configuration

/etc/
├── bash.bashrc
├── profile
└── profile.d/
├── lang.sh
├── wp-tools.sh
└── prompt.sh

✅ Recommendation: Keep personal prompt customization in ~/.bashrc or a dedicated file like ~/.bash_prompt, then source it from .bashrc.​

5. Understanding PS1 Structure​

PS1 is a formatted string that Bash expands dynamically every time a new command prompt appears.

Common Escape Sequences​

CodeMeaningExample Output
\uUsernameroot
\hHostnameGC-SG-M16GB
\wCurrent working directory/root
\WLast part of pathhtml
\tTime (HH:MM:SS)15:43:20
\dDateSun Oct 13
\#Command number42
\$$ (normal user) or # (root)#
Example:
PS1='\u@\h:\w\$ '

Output:

root@GC-SG-M16GB:/root#

6. Adding Colors​

Bash supports ANSI escape codes for color formatting. Color codes must be enclosed in [ and ] to prevent line wrapping issues.

Color Reference​

ColorCode
Red\e[31m
Green\e[32m
Yellow\e[33m
Blue\e[34m
Cyan\e[36m
Reset\e[0m

Example 1: Add Color to Prompt​

PS1='[\e[32m]\u@\h[\e[0m]:[\e[34m]\w[\e[0m]\$ '

Output:

root@GC-SG-M16GB:/root#

(with green username and blue directory)​

Example 2: Add Timestamp​

PS1='[\t] \u@\h:\w\$ '

Output:

[15:45:02] root@GC-SG-M16GB:/root#

7. WordPress VPS Prompt Customization​

You can differentiate environments visually for safety.

a. Production Server (Red Warning)​

PS1='[\e[31m][PROD: \u@\h \W]\$[\e[0m] '

Output:

[PROD: root@meta-prod html]#

b. Staging Server (Yellow Neutral)​

PS1='[\e[33m][STAGE: \u@\h \W]\$[\e[0m] '

Output:

[STAGE: ubuntu@wpstage html]$

c. Development Server (Green Safe Zone)​

PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '

Output:

[DEV: donny@local html]$

8. Making Prompt Persistent​

Add your PS1 configuration to one of the following:

a. User Level​

Edit ~/.bashrc:

nano ~/.bashrc

Add:

# Custom Prompt (WordPress VPS)
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '

Reload:

source ~/.bashrc

b. System-Wide​

Edit /etc/bash.bashrc:

sudo nano /etc/bash.bashrc

Add:

# Global PS1 for all users
export PS1='[\e[36m][SERVER: \u@\h \W]\$[\e[0m] '

Reload:

source /etc/bash.bashrc

c. Using a Dedicated Prompt File​

For cleaner management:

nano ~/.bash_prompt

Add:

export PS1='[\e[35m][WP-VPS: \u@\h \W]\$[\e[0m] '

Then reference it inside .bashrc:

if [ -f ~/.bash_prompt ]; then
source ~/.bash_prompt
fi

9. Advanced Prompt Examples​

a. Show Git Branch​

If using Git for WordPress deployment:

parse_git_branch() {
git branch 2>/dev/null | sed -n '/\* /s///p'
}
PS1='[\e[32m]\u@\h [\e[34m]\w[\e[33m]$(parse_git_branch)[\e[0m]\$ '

Output:

root@GC-SG-M16GB /var/www/html (main)#

b. Show Exit Status of Last Command​

PS1='[[\e[31m]\$?[\e[0m]] \u@\h:\w\$ '

If the last command failed (exit code != 0), you’ll see the error code highlighted in red.​

c. Add Load Average (for performance monitoring)​

PS1='[\u@\h [\e[36m]load:$(cut -d" " -f1 /proc/loadavg)[\e[0m] \W]\$ '

Output:

[root@GC-SG-M16GB load:0.14 html]#

10. Best Practices​

  1. Keep it simple — avoid heavy subshells or slow commands.
  2. Use color coding — helps visually distinguish environments.
  3. Add environment labels — DEV, STAGE, PROD prevent confusion.
  4. Document your PS1 — include comment headers in .bashrc.
  5. Test after login — use echo $PS1 to verify applied changes.
  6. Backup before editing system-wide files:
cp /etc/bash.bashrc /etc/bash.bashrc.bak

11. Troubleshooting​

ProblemLikely CauseFix
Colors display as [e[31m]Missing [ and ] wrappersUse [\e[31m] syntax
Prompt too slowRunning commands (like git) in PS1Simplify or cache values
Wrong PS1 after SSH.bashrc not sourcedAdd source ~/.bashrc in .bash_profile
Lost default promptMisdefined PS1Reset using PS1='\u@\h:\w\$ '
Overwritten by system config/etc/bash.bashrc overrides user configMove definition to .bashrc below sourcing lines

12. Quick Lab​

Objective: Create distinct PS1 prompts for different environments.

  1. Edit:
nano ~/.bash_prompt
  1. Add:
# Environment-based Prompt
if [[ $HOSTNAME == *"prod"* ]]; then
export PS1='[\e[31m][PROD: \u@\h \W]\$[\e[0m] '
elif [[ $HOSTNAME == *"stage"* ]]; then
export PS1='[\e[33m][STAGE: \u@\h \W]\$[\e[0m] '
else
export PS1='[\e[32m][DEV: \u@\h \W]\$[\e[0m] '
fi
  1. Reference it from .bashrc:
if [ -f ~/.bash_prompt ]; then
source ~/.bash_prompt
fi
  1. Apply:
source ~/.bashrc
  1. Verify:
echo $PS1

✅ Result: Each environment displays a distinct color-coded prompt.​

13. Glossary​

TermDefinition
PS1Main Bash prompt variable (defines how the shell prompt looks).
Escape SequenceCode to apply colors or styles (e.g., \e[31m for red).
\u, \h, \wPrompt variables for username, hostname, and working directory.
/etc/bash.bashrcSystem-wide Bash config file for all users.
SubshellChild shell session launched inside another shell.

14. Mini-Quiz​

  1. What does \u@\h:\w\$ represent in a PS1 string?
  2. Why must color escape sequences be enclosed in [ and ]?
  3. Which file applies system-wide prompt settings?
  4. How can you visually separate production from development servers?
  5. What happens if you place a heavy command (like git status) in PS1?

15. Summary​

  • PS1 controls your Bash command prompt appearance.
  • You can include user, host, directory, time, and colors dynamically.
  • Stored in ~/.bashrc (user) or /etc/bash.bashrc (system-wide).
  • Crucial for WordPress VPS safety — helps distinguish environments visually.
  • Use colors to separate DEV (green), STAGE (yellow), and PROD (red).
  • Always test changes and use proper [] syntax to avoid rendering errors.