$ run as a normal user# run as root (or with sudo)
Skills in this chapter
- Design a prompt with information and colour
Read the parts of PS1
PS1 holds text and backslash codes. bash replaces each code when it prints the prompt.
echo $PS1
[\u@\h \W]\$| Code | Shows |
|---|---|
\a | Bell (a beep) |
\d | The date, like "Sun Nov 08" |
\h | Host name up to the first dot |
\H | Full host name |
\j | Number of jobs in this shell |
\l | Name of the terminal device |
\n | New line |
\r | Carriage return |
\s | Name of the shell |
\t / \T | Time, 24-hour / 12-hour |
\@ | Time, 12-hour with AM/PM |
\A | Time, hours:minutes, 24-hour |
\u | User name |
\v / \V | bash version |
\w | Full current directory |
\W | Last part of the current directory |
\! | History number of this command |
\# | Number of commands in this session |
\$ | $ for a normal user, # for root |
\[ … \] | Start / end of invisible codes (colours, cursor moves) |
Try new prompt designs
Save the old prompt in a variable first. Then try new values; restore the old one any time.
ps1_old="$PS1"
PS1="\$ "
PS1="\a\$ "
PS1="\A \h \$ "
PS1="<\u@\h \W>\$ "
PS1="$ps1_old"Add colour
Colours are ANSI escape codes: \033[ + colour number + m. Put them between \[ and \] so bash knows they take no space on the line.
| Code | Text colour | Code | Background |
|---|---|---|---|
\033[0;30m | Black | \033[0;40m | Black |
\033[0;31m | Red | \033[0;41m | Red |
\033[0;32m | Green | \033[0;42m | Green |
\033[0;33m | Brown | \033[0;43m | Brown |
\033[0;34m | Blue | \033[0;44m | Blue |
\033[0;35m | Purple | \033[0;45m | Purple |
\033[0;36m | Cyan | \033[0;46m | Cyan |
\033[0;37m | Light grey | \033[0;47m | Light grey |
1;instead of0;gives the bright (bold) version:\033[1;31m= light red.\033[0mresets all colours. Always end with it.
PS1="\[\033[0;31m\]<\u@\h \W>\$\[\033[0m\] "
PS1="\[\033[0;41m\]<\u@\h \W>\$\[\033[0m\] "Watch out
Without \[ … \] around the colour codes, bash counts them as characters, and long lines wrap at the wrong place.
Note
In the past each kind of terminal had its own codes ("terminal confusion"). Today almost all terminal emulators understand the ANSI codes.
Move the cursor
Other escape codes move the cursor. With them you can draw a status bar with a clock at the top of the window.
| Code | Action |
|---|---|
\033[l;cH | Move the cursor to line l, column c |
\033[nA / \033[nB | Up / down n lines |
\033[nC / \033[nD | Forward / back n characters |
\033[2J | Clear the screen |
\033[K | Clear to the end of the line |
\033[s / \033[u | Save / restore the cursor position |
PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]<\u@\h \W>\$ "This saves the cursor, jumps to the top line, paints it red, writes the time in yellow, resets the colour and goes back.
Keep the new prompt
Put the PS1 line in ~/.bashrc, then run source ~/.bashrc.
PS1="\[\033[0;32m\]\u@\h\[\033[0m\]:\[\033[0;34m\]\w\[\033[0m\]\$ "
export PS1At work
Give each kind of server its own colour: green for test, red for production. Put the setting in /etc/profile.d/ on the server so every admin sees it.
Note
Today many people use tools like Starship or "powerline" prompts. They are built from the same escape codes.
Summary of the chapter
PS1 = prompt; backslash codes add data; \[\033[…m\] adds colour; save it in ~/.bashrc.
1Which codes show the user, the host and the full directory?
\u, \h and \w.
2Why must colour codes be inside \[ and \]?
They do not use space on the screen; without the brackets bash miscounts the line length.
3How do you make a prompt red?
Start it with \[\033[0;31m\] and end it with \[\033[0m\].