Prompt Customization (PS1) — Core Customization Technique in Bash
- Understand what the Bash prompt (PS1) is and how it controls your command-line interface.
- Learn how to customize your PS1 variable to show username, hostname, path, time, and colors.
- Discover where and how PS1 customization is stored (user-level vs system-wide) — shown in tree view.
- Apply WordPress VPS–specific prompt styles (e.g., production vs staging color cues).
- 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
| Reason | Description |
|---|---|
| Clarity | Identify which environment you’re in (root, user, staging, production). |
| Safety | Avoid destructive mistakes by labeling production servers visually. |
| Usability | Display working directory, time, or Git branch for convenience. |
| Professionalism | Creates a clean, structured workspace aligned with your workflow. |
| Example goal: |
[PROD: root@wpstrategist:/var/www/html]#
3. Where Prompt Settings Are Stored
| Scope | File | Applies To | When Loaded |
|---|---|---|---|
| User-specific | ~/.bashrc | Current user | Every new shell |
| System-wide | /etc/bash.bashrc | All users | Every new shell |
| Environment fallback | /etc/profile | Login shell | Before .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
| Code | Meaning | Example Output |
|---|---|---|
\u | Username | root |
\h | Hostname | GC-SG-M16GB |
\w | Current working directory | /root |
\W | Last part of path | html |
\t | Time (HH:MM:SS) | 15:43:20 |
\d | Date | Sun Oct 13 |
\# | Command number | 42 |
\$ | $ (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
| Color | Code |
|---|---|
| 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
- Keep it simple — avoid heavy subshells or slow commands.
- Use color coding — helps visually distinguish environments.
- Add environment labels —
DEV,STAGE,PRODprevent confusion. - Document your PS1 — include comment headers in
.bashrc. - Test after login — use
echo $PS1to verify applied changes. - Backup before editing system-wide files:
cp /etc/bash.bashrc /etc/bash.bashrc.bak
11. Troubleshooting
| Problem | Likely Cause | Fix |
|---|---|---|
Colors display as [e[31m] | Missing [ and ] wrappers | Use [\e[31m] syntax |
| Prompt too slow | Running commands (like git) in PS1 | Simplify or cache values |
| Wrong PS1 after SSH | .bashrc not sourced | Add source ~/.bashrc in .bash_profile |
| Lost default prompt | Misdefined PS1 | Reset using PS1='\u@\h:\w\$ ' |
| Overwritten by system config | /etc/bash.bashrc overrides user config | Move definition to .bashrc below sourcing lines |
12. Quick Lab
Objective: Create distinct PS1 prompts for different environments.
- Edit:
nano ~/.bash_prompt
- 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
- Reference it from
.bashrc:
if [ -f ~/.bash_prompt ]; then
source ~/.bash_prompt
fi
- Apply:
source ~/.bashrc
- Verify:
echo $PS1
✅ Result: Each environment displays a distinct color-coded prompt.
13. Glossary
| Term | Definition |
|---|---|
| PS1 | Main Bash prompt variable (defines how the shell prompt looks). |
| Escape Sequence | Code to apply colors or styles (e.g., \e[31m for red). |
| \u, \h, \w | Prompt variables for username, hostname, and working directory. |
| /etc/bash.bashrc | System-wide Bash config file for all users. |
| Subshell | Child shell session launched inside another shell. |
14. Mini-Quiz
- What does
\u@\h:\w\$represent in a PS1 string? - Why must color escape sequences be enclosed in
[and]? - Which file applies system-wide prompt settings?
- How can you visually separate production from development servers?
- 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.