bash: display CURRENT time in prompt?
After much too often wondering "hey, when did I actually start this time-consuming command?", I'm considering adding a timestamp to my bash prompt.
My first try was simple: PS1="(\A) $PS1" which gives a display like: (16:58) exscape ~ # However, the problem with that approach is that it displays the time when the line was written, not when the command on it was executed. So, if I run "ls" at 16:58, and get a new prompt "(16:58) ..." and then run the next command 2 hours later, it'll look like this: (16:58) exscape ~ # new-command I of course want the line to display the time when the command was actually run, not when the prompt appeared. Any ideas? |
Did you try running your time-consuming commands with 'time'?
I always assumed it's just logical that it goes the way you described..time is read, command prompt written, then you are expected to type in something etc. But you never know, maybe it is possible anyway; one way I can think of (I'm not sure if it's possible in practice, but as an idea) would be to wait till something is typed in, then "cut" that (so it's not lost), rewrite the command prompt with current time in it, then "paste" (to get the updated time show up) and run the command and see how it works out..then next prompt would be printed the way you made it etc. Sounds like a lot of work, though, unless you run time-consuming things all the time and don't want to type in extra five chars :) An example from the man page of time: Code:
time wc /etc/hosts |
Yeah, I do use 'time', but not always. The times I forget is when I'd like this! :)
(And even when using time, if it's mid-operation, you still have no clue when it started.) It definitely makes sense this way; the shell reads the clock, and prints it out. Period. What I'd like is, I guess, a clock that updates up until you press enter, when the time you pressed enter is conserved in the history/scrollback. |
Quote:
Code:
export HISTTIMEFORMAT="%Y%m%d %H:%M:%S " |
Hi.
I have not used this feature of zsh, but perhaps it may be of use in this situation: Quote:
|
preexec() looks like it could work, if I could only find a command to go up one line and overwrite the time. That'd mean switching to zsh, though... I've considered it for some time but I'm not sure I'm ready just yet. ;)
HISTTIMEFORMAT only appears to be used with the "history" command, no? It doesn't change my scrollback a bit, anyhow. Anyways, thanks for the answers, but more ones are still appreciated :) Especially a simple way to do "go up one line, to the beginning of the line, and type this" for preexec(). |
You could try the following:
PS1="(\$(date +%H:%M:%S) $PS1" I tested this on HP-UX and OpenSUSE 11.0 and it seems to give prompt you want. Regards, Petteri |
Quote:
Look at this output for example: Code:
(11:02) exscape ~ # ls |
$PS4 and set -x
I am posting this late because this comes up in the first page when searching for bash prompt timestamp, I hope it's useful:
Code:
user@hostname:~$ PS4=":\D{%F %T}: "; set -x |
just add ' \@ ' somewhere in your PS1
Example: export PS1='\[\033[0m\]\[\033[0;30;47m\] \u\[\033[0m\]\[\033[0;30;47m\]@\h\[\033[0m\] \w\n\[\033[1;32m\] \@ \$ \[\033[0m\]' ciao! |
sorry to necro-bump, but this thread is a google-result on the subject.
This script solves the problem in a fair enough way: https://github.com/rcaloras/bash-preexec Use as directed and set to something like Code:
preexec() { date; } Further coloring and enhancement might include: Code:
preexec() { echo $'\e[7m'$(date +"%b %d; %H:%M:%S")$'\e[m'; } |
If you're using tmux, you can put the time in the tmux status line. That works too.
|
All times are GMT -5. The time now is 06:49 PM. |