C.W.K.
Stream
Lesson 04 of 10 · published

Anatomy of the Prompt

~15 min · prompt, ps1, powerlevel10k, starship

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

What the prompt actually is

The prompt is a string the shell prints before each command. Bash stores it in $PS1, zsh in $PROMPT (or $PS1; both work). It is just text with escape codes — your shell evaluates these every time it gets ready for input. That means a great prompt is a few characters of expansion, not a plugin.

The escape codes you'll see most

  • %n / \u — username
  • %m / \h — short host
  • %~ / \w — current dir, with ~ for home
  • %? / $? — exit code of the last command
  • %F{red} / \[\033[31m\] — color start (zsh / bash)
  • %f / \[\033[0m\] — color reset

If you read someone's .zshrc and see PROMPT='%F{cyan}%n%f@%m %~ %F{red}%(?..%?)%f%# ', that decodes to: cyan username, regular host, working directory, red exit code only when non-zero, then the prompt character.

Multi-line prompts and right-side prompts

You can split the prompt onto two lines so commands always start at the left margin. Set PROMPT to one line ending in $'\n' and a short final glyph. Zsh also exposes RPROMPT, which prints on the right of the same line — perfect for git branch info that doesn't push your typing area around.

Themed prompts you don't write yourself

You don't have to hand-craft escape codes. Powerlevel10k (zsh) and Starship (any shell) generate fast, async, info-rich prompts: git status, Python venv, Node version, Kubernetes context, AWS profile, last-command duration. Write your own only if you enjoy it; otherwise pick Starship and move on.

Code

Minimal informative zsh prompt·zsh
# Add to ~/.zshrc
setopt PROMPT_SUBST
autoload -U colors && colors
PROMPT='%F{cyan}%n%f@%m %~ %F{red}%(?..[%?] )%f%# '
RPROMPT='%F{8}%*%f'   # right-aligned clock
Install Starship in 30 seconds·bash
brew install starship
echo 'eval "$(starship init zsh)"' >> ~/.zshrc
exec zsh   # reload

External links

Exercise

Install Starship (brew install starship, then eval "$(starship init zsh)" in .zshrc). Open a git repo, then cd into a Python project with a venv. Watch the prompt change automatically — branch indicator, Python icon, venv name. No scripting from you.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.