2

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?

1

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.

  • Hi, I wanted a live timer, that changes without pressing enter. – user2719875 Feb 26 at 2:01
  • Just realised that, but that means you need zle in zsh. Also, having had it, it was a pain having it keep refreshing the terminal, and I found checking history easier, and turned it off. – Alex Feb 26 at 2:04
  • Yea I am trying to avoid zsh too. It was slow for me when I used it with iterm2. Okay thanks. – user2719875 Feb 26 at 2:08
  • you can just add a digital clock to screen:) – jmh Feb 26 at 2:15
  • I just updated my answer with another answer but it requires you to use the z-shell. – jmh Feb 26 at 2:38
1

If you are using a bash shell, add the following line to the .bashrc file (not the .bash_profile file).: export PROMPT_COMMAND="echo -n \[\$(date +%H:%M:%S)\]\ "

I just tested this and it works fine. Gives you a prompt like so; [20:58:36]$

This question was asked on StackExchange Ask Ubuntu. The answer was given here. It requires using the z-shell which I know you wish to avoid. Maybe this method is faster.

  • Hey, does this change live? Like without pressing enter? – user2719875 Feb 26 at 2:10
  • no. every time you hit return it updates. – jmh Feb 26 at 2:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .