How to create custom bash prompts
· Category: Linux
Short answer
Modify the PS1 environment variable to change the appearance and content of your bash prompt.
Steps
- Basic color prompt:
export PS1="\[\e[32m\]\u@\h\[\e[0m\]:\w\$ "
- Show git branch:
export PS1='\u@\h \W\$(__git_ps1 " (%s)")\$ '
- Add timestamp:
export PS1="\t \u@\h:\w\$ "
- Make permanent in
~/.bashrc.
Tips
- Use
\[and\]around non-printing characters to prevent line wrapping issues. - Install
bash-git-promptorstarshipfor advanced prompts. - Test changes in a new terminal before saving to
.bashrc.
Common issues
- Forgetting escape brackets causes cursor misalignment.
- Complex prompts can slow down shell startup.