Hack 35. Display current time in the prompt

by Ramesh

In the PS1 environment variable, you can directly execute any Linux command, by specifying in the format $(linux_command). In the following example, the command $(date) is executed to display the current time inside the prompt.

  1. ramesh@dev-db ~> export PS1="\u@\h [\$(date +%H:%M:%S)]> "
  2.  
  3. ramesh@dev-db [11:09:56]>

You can also use \t to display the current time in the hh:mm:ss format as shown below:

  1. ramesh@dev-db ~> export PS1="\u@\h [\t]> "
  2.  
  3. ramesh@dev-db [12:42:55]>

You can also use \@ to display the current time in 12-hour am/pm format as shown below:

  1. ramesh@dev-db ~> export PS1="[\@] \u@\h> "
  2.  
  3. [04:12 PM] ramesh@dev-db>