Unix Power ToolsUnix Power ToolsSearch this book

4.2. Static Prompts

As Section 4.1 explains, the simplest prompts -- which I call static prompts -- are prompts whose value are set once. The prompt doesn't change (until you reset the prompt variable, of course).

The default bash prompt is a good example of a static prompt. It's "bash$ " (with a space at the end, to make the command you type stand out from the rest of the prompt). You could set that prompt with the simple command:

PS1='bash$ '.

Notice the single quotes (Section 11.3) around the value; this is a good idea unless you want special characters in the prompt value to be interpreted before it's set. You can try it now: type that command on a command line, just as you would to set any other shell variable. Experiment a bit. The same prompt works on ksh and sh.

If you use csh or tcsh, try one of these, then experiment:

set prompt='csh% '
set prompt='tcsh> '

(zsh users: you can use any of the previous styles, but omit the set from the set prompt style.) Those prompts are fairly useless, right? If you log in to more than one machine, on more than one account, it's nice to have your hostname and username in the prompt. So try one of the following prompts. (From here on, I won't show a separate tcsh version with a > instead of a %. You can do that yourself, though, if you like.) If your system doesn't have uname, try hostname instead:

PS1="$USER@`uname -n`$ "
set prompt="$user@`uname -n`% "

Notice that I've used double quotes (Section 12.3) around the values, which lets the shell expand the values inside the prompt string before the prompt is stored. The shell interprets the variable $USER or $user -- and it runs the command substitution (Section 28.14) that gives the hostname -- once, before the prompt is set. Using double quotes is more efficient if your prompt won't change as you move around the system.

--JP and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.