Static prompts, such as the example in the preceding section, are kind of boring, so bash lets you include special character sequences (we’ll call them macros) that represent changing data. Each macro starts with a backslash and is followed by a single character that tells bash which chunk of data you want to dis- play. For example, if you want to display the current date and time whenever the prompt is displayed, use the \d(date) and \t(time) macros like this:
[freddie@bastille] PS1=”\d \t “ Thu Dec 18 03:37:48
TABLE4-1: HANDYMACROS FORYOURPROMPT
Macro What It Does/Displays Timesaving Bonus Info
\a Speaker beep To keep users on their toes, code the $PS2variable to beep when the user needs to input additional information. Just enter $PS2=”\a >”, and the computer beeps when it needs attention! \d Weekday (Sun–Sat), month name, and date Handy when you’re pulling all-nighters and you need to know
(“Thu Dec 18”, for example) when Saturday morning rolls around. 07 571737c04.qxd 7/2/04 7:59 PM Page 24
Adding Dynamically Updated Data to Your Prompt
25
Macro What It Does/Displays Timesaving Bonus Info
\D{} Date and/or time in a format of your choosing The \Dmacro must be followed by a format string enclosed in braces. bash interprets the format string by using the same rules as the strftimelibrary function (see man strftimefor more details). If the format string is empty, the braces are still required, but bash chooses a display format appropriate to your locale. \e Escape character; used for complex strings Escape characters introduce complex, unfriendly terminal
command sequences. We show you a better way later in this technique.
\h Host name up to the first .(dot) If you work on a number of different hosts from the same work- station, \hcan help you remember which one you’re currently connected to.
\H Entire host name Similar to \h, but takes up too much screen real estate for our taste.
\n Newline Use a new line to create a multiline prompt.
\s Shell name — such as bash or csh We’ve never found a particularly good use for this one because we always stick to bash.
\t Current time in 24-hour (HH:MM:SS) format \T Current time in 12-hour (HH:MM:SS) format
\@ Current time in 12-hour (am/pm) format Is it 5:00 yet? \A Current time in 24-hour (HH:MM) format
\u Current user name Include \uif you need to do work on someone else’s behalf (in other words, if you’re an administrator). That way you won’t for- get who you are and send flaming e-mail using someone else’s name!
\W Trailing component of your current This is probably the most useful macro you could include in a working directory custom prompt — sort of a “You Are Here” sign.
\w Entire current working directory Similar to \W, but takes up a lot of room on your command line. \\ Backslash character
\! History number Every command that you execute is stored in a history log, and you can refer to a specific command in the log by its history num- ber. Include the \!macro in your prompt, and you’ll see the his- tory number assigned to each command. (We talk more about history processing in Technique 9.)
\$ If the effective UID is 0, a #; otherwise a $ The \$macro displays a pound sign (#) if you hold superuser privileges or a dollar sign ($) if you don’t. You can use the \$ macro to help you remember when you have enough privileges to seriously damage your system, but we show you a better way in the section “Seeing a Red Alert When You Have Superuser Privileges,” later in this technique.
Technique 4: Prompting Yourself with a Custom Prompt
26
knows the right escape sequences, so you don’t have to spend time looking them up. The blue prompt in the preceding example looks like this when you use tput:
[freddie@bastille] BLUE=$(tput setaf 4) [freddie@bastille] BLACK=$(tput setaf 0) [freddie@bastille] PS1=”\[$BLUE\]\u@\h]\
[$BLACK\] “ [freddie@bastille]
The first line uses tputto find the character sequence that changes the foreground color to blue. The sec- ond line finds the character sequence that changes the foreground color to black. Notice that you don’t need to know the magic escape sequences; tput keeps a database of terminal descriptions and con- sults that database to find the sequence that corre- sponds to the terminal (or terminal emulator) you’re using. The third line patches the $BLUEand $BLACK sequences into the $PS1prompt string.
The $PS1string, however, is still more complicated than it needs to be — it’s got a few extra \[and \] sequences. Those extra characters are required so that bash knows which prompt characters take up screen real estate and which ones don’t (the invisible characters must appear between a \[and \]pair).
When you use tput, you can clean up extra characters a bit more by including those extra characters in the $BLUEand $BLACKvariables: [freddie@bastille] BLUE=”\[$(tput setaf
4)\]”
[freddie@bastille] BLACK=”\[$(tput setaf 0)\]”
[freddie@bastille] PS1=”$BLUE\u@\h]$BLACK “ [freddie@bastille]
tputcan do much more than just change the foreground color of the prompt. Table 4-2 shows a few of the more use- ful tputsequences. (For a complete list, see man tputand man terminfo.)