blogpost
Add a timestamp to your Bash prompt

Written by Bram Neijt on .

Some things on my Raspberry PI take some time, so I have to things I added to cope with this. The first thing is use screen. This allows you to start a terminal session, detach it and then come back to it at a later date. If you want to start quickly: type screen to start a session, user ctrl-a followed by d to detach it, and screen -r to resume your screen session. You can read more about screen on the Arch Linux wiki.

Another thing I did is add a timestamp to my bash prompt. Your prompt is defined by the environment variable PS1 and defined in the file ~/.bashrc. A common default version on Arch Linux is:

PS1='[\u@\h \W]\$ '

This will make the prompt say [user@hostname currentdirectory]$. You can use quite a few different escape sequences. The full list comes from reading man bash, at the section PROMPTING:

\a     an ASCII bell character (07)
\d     the  date  in "Weekday Month Date" format (e.g., "Tue May
       26")
\D{format}
       the format is passed to strftime(3)  and  the  result  is
       inserted  into the prompt string; an empty format results
       in a locale-specific time representation.  The braces are
       required
\e     an ASCII escape character (033)
\h     the hostname up to the first `.'
\H     the hostname
\j     the number of jobs currently managed by the shell
\l     the basename of the shell's terminal device name
\n     newline
\r     carriage return
\s     the  name  of  the shell, the basename of $0 (the portion
       following the final slash)
\t     the current time in 24-hour HH:MM:SS format
\T     the current time in 12-hour HH:MM:SS format
\@     the current time in 12-hour am/pm format
\A     the current time in 24-hour HH:MM format
\u     the username of the current user
\v     the version of bash (e.g., 2.00)
\V     the release of bash, version + patch level (e.g., 2.00.0)
\w     the current working  directory,  with  $HOME  abbreviated
       with  a tilde (uses the value of the PROMPT_DIRTRIM vari‐
       able)
\W     the basename of the current working directory, with $HOME
       abbreviated with a tilde
\!     the history number of this command
\#     the command number of this command
\$     if the effective UID is 0, a #, otherwise a $
\nnn   the character corresponding to the octal number nnn
\\     a backslash
\[     begin  a sequence of non-printing characters, which could
       be used to embed a terminal  control  sequence  into  the
       prompt
\]     end a sequence of non-printing characters

Because I can start things in screen which take more then a day on my Raspberry PI, I decided to go with a full date. This means I read up on strftime formatting by running man 3 strftime. I decided to use the following two escape sequences:

%F     Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
%T     The time in 24-hour notation (%H:%M:%S). (SU)

This means my complete PS1 variable value now looks like this:

PS1='[\u@\h \W] \D{%F %T}\n\$ '

Resulting in prompt like:

[bram@besje ~] 2013-02-14 20:57:03
$ 

You can see, the \n for the newline and the date and time behind the command prompt. Now you never have to wonder how long your Raspberry PI has been working on something after you resume your screen session!

Please note that Google uses cookies to track you on this site.