The Bourne shell's will run one or more commands when the shell gets a (usually, from the kill command). The shell will run any command, including commands that set shell variables. For instance, the shell could re-read a configuration file; article 38.11 shows that. Or it could set a new PS1 prompt variable that's updated any time an external command (like another shell script or a ) sends the shell a signal. There are lots of possibilities.
This trick takes over signal 5, which usually isn't used. When the shell gets signal 5, a trap runs a command to get the date and time, then resets the prompt. Ajob springs this trap once a minute. So, every minute, after you type any command, your prompt will change.
You could run any command: count the number of users, show the 7.8 has an example. But, to have an external command update a shell variable at any random time, this trap trick is still the best., whatever. And newer shells, like bash, can run a command each time the prompt is displayed - article
Now on to the specific example of putting date and time in the old
Bourne shell's prompt.
If your system's date command doesn't understand date formats
+%a), get one
that does - like the
Put these lines in your
file (or just type them in at a Bourne shell prompt):
# Put date and time in prompt; update every 60 seconds: trap 'PS1=`date "+%a %D %H:%M%n"`\ $\ ' 5 while : do sleep 60 kill -5 $$ done & promptpid=$!
Now, every minute after you type a command, your prompt will change:
Mon 02/17/92 08:59 $
cc bigprog.cundefined symbol first referenced in file xputc bigprog.o ld fatal: Symbol referencing errors. Mon 02/17/92 08:59 $
lsbigprog.c bigprog.o Mon 02/17/92 09:00 $
The prompt format is up to you.
This example makes a
with backslashes (
\) to protect the newline and space from the
trap; a single-line prompt might be easier to design.
The manual page for
lists what you can put in the prompt.
at a prompt or put it in a file that's.