Sample Header Ad - 728x90

Show live clock in bash in front of command prompt

4 votes
1 answer
576 views
I am looking for a way to show the current live time/clock in bash before the command prompt. What I researched so far, there are some possibilities for that. E.g. I know the bash variable PS1 can be customized with \t but that only updates the clock every time you press enter. For example one can put this in ~/.bashrc what I currently have: PS1='[\t] \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$ \[\033[00m\]' But that is not what I want - my goal is something like shown here which uses something like following code what I also have in my ~/.bashrc: while sleep 1;do tput sc;tput cup $(($(tput lines)-1)) 0;printf [date +%H:%M:%S];tput rc;done & This is nearly perfect for me, the only problem with this code is, it works well until I send SIGINT e.g. by pressing control+c. I would like a solution like that which does not stop the clock (or that particular subshell where the clock runs) when pressing control+c. I want to be able to use control+c like usual in bash. To be clear, I also found a solution for this with zsh but I would like to do that with bash if anyhow possible. And I do not want the clock to be shown elsewhere in the terminal , it should be shown live at the prompt, so I can e.g. even scroll back up the terminal and look at what time/second exactly I issued a command. Thanks in advance!
Asked by Christoph (273 rep)
Jul 28, 2024, 10:29 PM
Last activity: Jul 30, 2024, 06:46 AM