Many shells can interpret the stored prompt string as each prompt is printed. As Section 4.1 explains, I call these dynamic prompts.
Special character sequences in the prompt let you include the current directory, date and time, username, hostname, and much more. Your shell's manual page should list these at the PS1 or prompt variable. (If you use the Korn shell or the original C shell, you don't have these special sequences. Section 4.4 has a technique that should work for you.)
It's simplest to put single quotes around the prompt string to prevent
interpretation (Section 27.1) as the prompt is stored.
For example, the following prompt shows the date and time, separated by spaces.
It also has a special sequence at the end (\$
in bash,
%#
in tcsh and zsh) that's printed
as a hash mark (#
) if you're the superuser,
or the usual prompt character for that shell otherwise. The first command in the
following code listing works only in bash;
the second only in tcsh:
PS1='\d \t \$ ' ...bash set prompt='%w %D %Y %P %# ' ...tcsh PS1='%W %* %# ' ...zsh
Having the history number in your
prompt, as Section 4.14 shows, makes
it easy to use history (Section 30.8) to repeat or modify a
previous command. You can glance up your screen to the prompt where you ran the
command, spot the history number (for example, 27), and type !27
to repeat it, !27:$
to grab the filename off the end of the line, and much
more. In csh, tcsh, and bash prompts, use
\!
to get the history number. In
zsh, use %!
instead.
—JP, TOR, and SJC