Hack 35. Display current time in the prompt

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.

ramesh@dev-db ~> export PS1="\u@\h [\$(date +%H:%M:%S)]> "

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:

ramesh@dev-db ~> export PS1="\u@\h [\t]> "

ramesh@dev-db [12:42:55]>

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

ramesh@dev-db ~> export PS1="[\@] \u@\h> "

[04:12 PM] ramesh@dev-db>

Comments on this entry are closed.

  • Will Charlton October 29, 2013, 12:19 pm

    Great post! But I’ve been trying to find one that keeps the time updated in the prompt. In other words, the timestamp that is displayed in the prompt with these three methods shows the time as of when the prompt was printed. I’d like to see the time keep on ticking in the prompt =)

    Thanks,

    ~Will