Including Date/Time in the Linux Prompt

The basic way to set a prompt is by setting the “PS1” variable.

In Linux (the BASH shell, in particular), the default format is:

export PS1="[\u@\h \W]\$ "

That will give you a prompt that looks like this (where “dir” is your current directory)

[username@host dir]$

We can add a date/timestamp to it by changing the prompt to this:

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

So, for today (August 21, 2024 at 14:45:00), the prompt would look like this:

2024-08-21 14:45:00 [user@host ~]$

So, why would I want to set my prompt this way?

Generally, I wouldn’t. I tend to like things pretty clean.

But, if I’m doing a big upgrade with a lot of steps, it would be nice. Combine that with a terminal emulator that logs everything and you have a pretty decent way to capture “start/stop” times for individual commands.

You could even put some sort of token at the front to make the lines easier to grep out of the log. This one would put “[CMD] ” at the front of each line:

export PS1="[CMD] \D{%F %T} [\u@\h \W]\$ "

Keep in mind though. This prompt is not “dynamic”. It will only show the time at the instant that the prompt is printed.