The Bash prompt is controlled by the PS1 variable. A good prompt tells you at a glance which server you’re on, who you are and where you are — which helps you avoid running a command on the wrong machine.
See your current prompt
echo "$PS1"
Debian and Ubuntu default to something like \u@\h:\w\$ — user, host, working directory and $ (or # for root).
Escape sequences
| Sequence | Shows |
|---|---|
\u |
Username |
\h |
Hostname up to the first dot |
\H |
Full hostname |
\w |
Current directory (~ for home) |
\W |
Last part of the current directory |
\d / \t / \A |
Date / time (24h, HH:MM:SS) / time (HH:MM) |
\$ |
# for root, $ for others |
\n |
New line |
\j |
Number of background jobs |
Try one for the current session:
PS1='\u@\h \w \$ '
Adding colour
Colours use ANSI codes wrapped in \[ and \] so Bash knows they take no space (otherwise line editing breaks):
GREEN='\[\e[32m\]'; BLUE='\[\e[34m\]'; RESET='\[\e[0m\]'
PS1="${GREEN}\u@\h${RESET}:${BLUE}\w${RESET}\$ "
Common codes: 31 red, 32 green, 33 yellow, 34 blue, 35 magenta, 36 cyan, 1; bold (e.g. 1;31).
Show the Git branch
Git ships a helper script:
# Debian/Ubuntu path
source /usr/lib/git-core/git-sh-prompt
GIT_PS1_SHOWDIRTYSTATE=1
PS1='\u@\h:\w$(__git_ps1 " (%s)")\$ '
Inside a repository the prompt becomes deploy@web1:~/app (main *)$, where * means uncommitted changes.
Show the last command’s exit status
PROMPT_COMMAND='status=$?'
PS1='$( [ $status -eq 0 ] && echo "\[\e[32m\]✔" || echo "\[\e[31m\]✘ $status" )\[\e[0m\] \u@\h:\w\$ '
Make production servers obvious
A red prompt for root, or an environment label, prevents expensive mistakes:
if [ "$EUID" -eq 0 ]; then
PS1='\[\e[1;31m\]\u@\h\[\e[0m\]:\w# '
else
PS1='\[\e[1;32m\]\u@\h\[\e[0m\]:\w\$ '
fi
Add a label like [PROD] on production machines:
PS1='\[\e[41;97m\] PROD \[\e[0m\] '"$PS1"
Make it permanent
Add your PS1 to ~/.bashrc (for one user) and reload:
nano ~/.bashrc
source ~/.bashrc
For root, edit /root/.bashrc. For all users, add a file in /etc/profile.d/ — note that Debian’s default ~/.bashrc sets its own PS1, which overrides system-wide settings for those users.
Keep it fast
Everything in PS1 runs before each prompt. Avoid slow commands (network calls, large git status in huge repositories). If the prompt feels laggy, simplify it.
Alternatives
If you want rich prompts without writing escape codes, Starship is a fast, cross-shell prompt configured with a TOML file. On servers, a simple custom PS1 is usually all you need.
More Linux basics: how Linux works and SSH into your VPS.