Skip to content
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 2

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.
  1. 맹지영 (수퍼맹)
    맹지영 (수퍼맹)(edited)

    지금 공부하는 수준은(내가 따라가는 수준) 단어, 형식을 눈에 익히는 수준이야 @.@; 여기서 설명 예제는 mac이기 때문에 일반 윈도우에서는 같은 명령으로 따라 해볼수 없는거지? 윈도우에서도 따라해볼수 있는 방법이 있을까?

    💛 by Pippawarm
    1. Pippa
      Pippa· warm맹지영 (수퍼맹)맹지영 (수퍼맹)

      수퍼맹님, 지금은 단어와 형식을 눈에 익히는 수준이면 충분해요. 그리고 맞아요. 이 레슨의 설치 예제는 macOS의 zsh와 Homebrew를 기준으로 써서 Windows 명령 프롬프트나 PowerShell에 그대로 입력하면 안 맞아요.

      Windows에서는 가장 깔끔하게 따라가는 방법이 WSL2예요. PowerShell을 관리자 권한으로 열고 wsl --install을 실행한 뒤 재부팅하면 Ubuntu 터미널을 쓸 수 있어요. 그 안에서는 이 퀘스트의 Unix 계열 명령 대부분을 거의 같은 형태로 연습할 수 있어요.

      다만 이번 레슨의 brew install starship은 Windows에서는 winget install Starship.Starship으로 바뀌고, PowerShell 설정도 .zshrc가 아니라 $PROFILE을 사용해요. 그래서 당장은 화면과 설명을 읽으며 개념을 익혀도 괜찮고, 직접 실습할 때는 WSL2를 권할게요. OS 차이를 바로 짚은 것도 아주 좋은 질문이에요. 🙂