Customizing the prompt
Build a Bash prompt from PS1 escapes, add colour without breaking line wrapping, show the git branch with a function, and use PROMPT_COMMAND.
Variables and the environmentCommand and arithmetic substitutionAliases and shell functions in rc files
Customizing the prompt
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The prompt is the value of PS1, and Bash expands backslash escapes in it each time it is drawn: \u user, \h host, \w working directory, \W its last part, \t time, \$ for $ or #.
- Wrap every colour escape in \[ and \] so readline knows those bytes take no screen width. Without them, long lines wrap onto themselves.
- A single-quoted PS1 containing $(...) re-runs the command every time the prompt is drawn, which is how a git branch or exit status gets in.
- PROMPT_COMMAND is a command Bash runs just before drawing the prompt, and PROMPT_DIRTRIM=2 shortens long paths to their last two directories.
- A prompt that runs slow commands slows every Enter press. Keep what it runs fast.
The prompt is a variable
The few characters before your cursor look like part of the terminal. They are not. The shell draws them, from the value of a variable called PS1, before it reads each command. Change the variable and the prompt changes at once. The default in a fresh Bash 5.3 session on macOS was bash-5.3$ , which is \s-\v\$ .
A prompt is worth tuning because it is the one piece of the interface you look at hundreds of times a day. Putting the current directory, the git branch or a failed exit status there saves you typing pwd and git branch and echo $?.
I tested all of this in an interactive Bash 5.3 on macOS with a scratch Git repo. The output below has my real username and hostname replaced by me and host.
Escapes
Inside PS1, a backslash plus a letter is replaced each time the prompt is drawn:
$ PS1='\u@\h:\w\$ '
me@host:/private/tmp/pr1$ PS1='\W \t \$ '
pr1 02:46:55 $ PS1='\w\$ '
/private/tmp/pr1$
\u is the user, \h the host, \w the working directory, \W only its last part, \t the time, and \$ prints $ for a normal user and # for root. Bash shows the home directory as ~. The full list is in the PROMPTING section of man bash.
Long paths eat the line. PROMPT_DIRTRIM trims them:
/tmp/pr1/deep/er/tree$ PROMPT_DIRTRIM=2
.../er/tree$
To preview a prompt without redrawing it, Bash 4.4 and newer can expand one for you with ${PS1@P}. The macOS system /bin/bash is 3.2 and does not have that, so check bash --version.
Colour and the bracket rule
Colour comes from escape sequences, bytes your terminal interprets instead of printing. \e[33m means yellow and \e[0m resets. The problem is that readline counts bytes to know how wide the prompt is, and a colour sequence takes bytes but no screen space. Wrap it in \[ and \] to say so:
PS1='\[\e[33m\]$(git_branch)\[\e[0m\] \w\$ '
You can see Bash handing the markers on to readline if you expand that prompt (with the git_branch function from the next section) and show the control characters:
$ echo "${PS1@P}" | cat -v
^A^[[33m^Bmain^A^[[0m^B /tmp/pr1$
^A and ^B are readline’s “start ignoring” and “stop ignoring” bytes, and ^[[33m is the colour sequence between them. Leave the brackets out and the prompt looks fine until you type a long command or press Up, and then the line wraps over itself. I did not reproduce that wrapping bug, because a captured screen cannot show it. This is the standard explanation, and the markers above are the part I verified.
A prompt that runs commands
PS1 can contain $(...). If it is in single quotes, Bash stores it as text and runs it each time the prompt is drawn. With a small function in an rc file:
git_branch() { git branch --show-current 2>/dev/null; }
$ PS1='$(git_branch)\$ '
main$ git switch -q -c feature
feature$ git switch -q main
main$
The branch follows you. Outside a repo, git_branch prints nothing, and the prompt is just $. The mistake that bites is double quotes: PS1="$(git_branch)\$ " runs the command once, as the line is read, so the prompt would say main forever. This uses command substitution and the quoting rules from the quoting lesson.
A function can show the last exit status only when it is the first thing run, since $? is replaced by every command. Here it reads $? before anything else and prints nothing on success:
prompt_status() { local s=$?; [ "$s" -ne 0 ] && printf '[%s] ' "$s"; }
$ PS1='$(prompt_status)\$ '
$ false
[1] $ true
$
PROMPT_COMMAND and the others
PROMPT_COMMAND holds a command Bash runs before it draws each prompt, which suits things that are not part of the prompt itself (updating a terminal title, saving history). I set it to echo the status:
$ PROMPT_COMMAND='echo "[last status $?]"'
[last status 0]
$ true
[last status 0]
$ false
[last status 1]
Bash has three other prompt variables, shown here with their defaults:
PS2=[> ] PS4=[+ ]
PS2 is what you see while a command continues onto another line, for example after an unclosed quote, and PS4 is the prefix set -x prints before each traced command (the debugging lesson uses it).
Keep it quick. The prompt runs before every Enter, so a command in it that takes half a second makes the whole shell feel slow. A git status in a big repo is the usual culprit.
About the video
“How to Customize Your Bash Prompt (PS1) - Complete Linux Tutorial” is a 20-minute video from Learn Linux TV (Jay LaCroix), published December 2025. The description says it explains how the PS1 prompt works, where to set a custom one, points to prompt generator tools, and shows his own prompt, with a short look at backgrounding processes. I could not read a transcript and I do not know how it covers the bracket rule above, so watch for that. It is an English-language channel and a recent video, though it is made for Linux. Prompt generator websites produce a PS1 line you can paste into an rc file. Read the line before you paste it.
Where this fits
You can now change what the shell says to you. The next lesson is about what it does when you press Tab: tab completion.
Check yourself
Why must colour escapes in PS1 be wrapped in \[ and \]?
Readline measures the prompt's width to place the cursor and wrap long lines. The brackets mark bytes that print nothing, and without them its count is wrong.
You put PS1="$(git branch --show-current) \$ " in your rc file and the branch never changes. Why?
The double quotes ran the command once, when the line was read. Use single quotes so $(...) is stored in PS1 and runs each time the prompt is drawn.
How do you see what a PS1 will look like without waiting for the next prompt?
Run echo "${PS1@P}" in Bash 4.4 or newer, which expands the prompt escapes in the value. macOS ships Bash 3.2, which does not have it.
What does PROMPT_DIRTRIM=2 do?
It limits \w to the last two directory components and replaces the rest with ..., so a deep path does not fill the line.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.