How to display timestamp on bash command prompt?

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux
  • bash

Issue

  • How to display timestamp on bash command prompt?

Resolution

To display timestamp on command prompt, use the special character \D{} for PS1 shell variable. It is possible to display arbitrary time format by putting a special character based on strftime() function into {}.

For example, to set up a command prompt which has [<USERNAME>@<HOSTNAME> <YYYYMMDD-hh:mm:ss>]$ format, issue the following:

PS1="[\u@\h \D{%Y%m%d-%H:%M:%S}]\$ "

For more information on special characters, refer to the online manual “STRFTIME(3) man 3 strftime.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments