home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 7.7 A "Menu Prompt" for Naive Users Chapter 7
Setting Your Shell Prompt
Next: 7.9 Show Subshell Level with $SHLVL
 

7.8 Highlighting in Shell Prompts

If your prompt has some information that you want to stand out - or if you want your whole prompt to stand out from the rest of the text on the screen - you might be able to make it in enhanced characters. If your terminal has special escape sequences ( 5.8 ) for enhancing the characters (and most do), you can use them to make part or all of your prompt stand out.

csh_init
Let's say that you want to make sure people notice that they're logged in as root (the superuser) by making part of the root prompt flash. Here are lines for the root .cshrc : sh_init




echo...033
 

uname -n
 

# Put ESCape character in $e.  Use to start blinking mode (${e}[5m)
# and go back to normal mode (${e}[0m) on VT100-series terminals:
set e="`echo x | tr x \\033`"
set prompt="${e}[5mroot${e}[0m@`uname -n`# "

That prompt might look like this, with the word root flashing:

root@sys.ora.com#

The prompt is set inside double quotes ( " ), so the uname -n command is run once, when the PS1 string is first stored. In some shells, like bash and pdksh , you can put single quotes ( ' ) around the PS1 string; this stores the backquotes ( ` ) in the string, and the shell will interpret them before it prints each prompt. (In this case, that's useless because the output of uname -n will always be the same. But if you want constantly updated information in your prompt, it's very handy.) Article 8.14 tells more.

Because the same escape sequences won't work on all terminals, it's probably a good idea to add an if test ( 47.3 ) that only sets the prompt if the terminal type $TERM is in the Digital Equipment Corporation VT100 series (or one that emulates it). Table 7.1 shows a few escape sequences for VT100 and compatible terminals. The ESC in each sequence stands for an ESCape character.

Table 7.1: VT100 Escape Sequences for Highlighting
Sequence What it Does
ESC[1m Bold, intensify foreground
ESC[4m Underscore
ESC[5m Blink
ESC[7m Reverse video
ESC[0m All attributes off

Of course, you can use different escape sequences if your terminal needs them. Better, read your terminal's terminfo or termcap database with a program like tput or tcap ( 41.10 ) to get the correct escape sequences for your terminal. Store the escape sequences in shell variables ( 6.8 ) .

bash interprets octal character strings in the prompt. So you can simplify the two commands above into the version below. Change the backquotes ( `...` ) to $( and ) ( 9.16 ) if you want:

PS1="\033[5mroot\033[0m@`uname -n`# "

Eight-bit-clean versions of tcsh can put standout, boldface, and underline - and any other terminal escape sequence, too - into your shell prompt. For instance, %S starts standout mode and %s ends it; the tcsh manpage has details for your version.

For example, to make the same prompt as above with the word root in standout mode ( tcsh puts the hostname into %m ):

set prompt = '%Sroot%s@%m# '

- JP


Previous: 7.7 A "Menu Prompt" for Naive Users UNIX Power Tools Next: 7.9 Show Subshell Level with $SHLVL
7.7 A "Menu Prompt" for Naive Users Book Index 7.9 Show Subshell Level with $SHLVL

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System