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

  1. Basic color prompt:
export PS1="\[\e[32m\]\u@\h\[\e[0m\]:\w\$ "
  1. Show git branch:
export PS1='\u@\h \W\$(__git_ps1 " (%s)")\$ '
  1. Add timestamp:
export PS1="\t \u@\h:\w\$ "
  1. Make permanent in ~/.bashrc.

Tips

  • Use \[ and \] around non-printing characters to prevent line wrapping issues.
  • Install bash-git-prompt or starship for 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.