MacOS – How to show current time in mac terminal prompt

bashcommand linemacosterminal

I have this:

PS1+="\D{%H:%M:%S}";

But this displays the time the prompt started. I want the prompt to show the current 12 hour (not 24 hours) time live (so it keeps changing, and then when a command is made, save that time before going to the next prompt (so, without pressing enter / return on the keyboard). So the time the command was made is saved and visible too.

Is this live updating of a time-based prompt possible?

Best Answer

Simple answer: \T is the code for 12hr time in HHH:MM:SS. So, you'll get:

$ export PS1+="\T"
08:56:25 $
08:56:26 $
08:56:28 $
08:56:30 $

In the case you have the PS1+ is appending onto an earlier configured PS1 variable - you'll need to modify to your needs.

This is a live variable in my version of bash on High Sierra. I copied and pasted above hitting enter to show the seconds incrementing.