Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Chapter 13Name in the book: Customizing the Prompt

Customize the prompt

The prompt is defined by one variable: PS1. With a few special codes you can show the time, the host name, the folder, colours and even a status line at the top of the terminal. A good prompt is also useful at work: a red prompt on production servers warns you before you type something dangerous.

  • 6 Lessons
  • 1 Commands
  • 1 Labs

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Design a prompt with information and colour

13.1

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]\$
CodeShows
\aBell (a beep)
\dThe date, like "Sun Nov 08"
\hHost name up to the first dot
\HFull host name
\jNumber of jobs in this shell
\lName of the terminal device
\nNew line
\rCarriage return
\sName of the shell
\t / \TTime, 24-hour / 12-hour
\@Time, 12-hour with AM/PM
\ATime, hours:minutes, 24-hour
\uUser name
\v / \Vbash version
\wFull current directory
\WLast 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)

Commands in this lesson

13.2

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"

13.3

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.

CodeText colourCodeBackground
\033[0;30mBlack\033[0;40mBlack
\033[0;31mRed\033[0;41mRed
\033[0;32mGreen\033[0;42mGreen
\033[0;33mBrown\033[0;43mBrown
\033[0;34mBlue\033[0;44mBlue
\033[0;35mPurple\033[0;45mPurple
\033[0;36mCyan\033[0;46mCyan
\033[0;37mLight grey\033[0;47mLight grey
  • 1; instead of 0; gives the bright (bold) version: \033[1;31m = light red.
  • \033[0m resets 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.

13.4

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.

CodeAction
\033[l;cHMove the cursor to line l, column c
\033[nA / \033[nBUp / down n lines
\033[nC / \033[nDForward / back n characters
\033[2JClear the screen
\033[KClear to the end of the line
\033[s / \033[uSave / restore the cursor position
a prompt with a clock bar at the top
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.

13.5

Keep the new prompt

Put the PS1 line in ~/.bashrc, then run source ~/.bashrc.

added to ~/.bashrc
PS1="\[\033[0;32m\]\u@\h\[\033[0m\]:\[\033[0;34m\]\w\[\033[0m\]\$ "
export PS1

At 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.

13.6

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\].

Practice

All commands in this chapter