LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash: display CURRENT time in prompt? (http://www.linuxquestions.org/questions/linux-software-2/bash-display-current-time-in-prompt-698120/)

exscape 01-18-2009 10:04 AM

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?

b0uncer 01-18-2009 11:42 AM

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

exscape 01-18-2009 11:46 AM

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.

colucix 01-18-2009 12:56 PM

Quote:

Originally Posted by exscape (Post 3412916)
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.

You can define a HISTTIMEFORMAT so that every time you look at the history of commands they will be timestamped. For example:
Code:

export HISTTIMEFORMAT="%Y%m%d %H:%M:%S "
You can customize the time format according to the POSIX standard, e.g. see the format of the command date.

makyo 01-18-2009 12:57 PM

Hi.

I have not used this feature of zsh, but perhaps it may be of use in this situation:
Quote:

preexec
Executed just after a command has been read and is about to be
executed. If the history mechanism is active (and the line was
not discarded from the history buffer), the string that the user
typed is passed as the first argument, otherwise it is an empty
string. The actual command that will be executed (including
expanded aliases) is passed in two different forms: the second
argument is a single-line, size-limited version of the command
(with things like function bodies elided); the third argument
contains the full text that is being executed.

-- excerpt from man zshall
cheers, makyo

exscape 01-19-2009 06:25 AM

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().

pastipet 01-19-2009 07:01 AM

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

exscape 01-19-2009 07:06 AM

Quote:

Originally Posted by pastipet (Post 3413715)
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

Thanks, but that still has the same flaw.
Look at this output for example:
Code:

(11:02) exscape ~ # ls
cap.rb  DMESG_ERRORS  gparted_details.htm restart-cleaned-libs.sh  ups-info
(13:19) exscape ~ #

As you can see, I ran ls at 13:19, but if you look at the ls command line, it says 11:02 - since the terminal had been idle since then. I want THAT line to display the time when the command on it was executed.

Mihai Cilidariu 04-09-2011 09:55 PM

$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
user@hostname:~$ true
:2011-04-10 05:35:00: true
user@hostname:~$

Unfortunately this conflicts with completion when you press TAB.

damko 07-23-2012 05:03 AM

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!


All times are GMT -5. The time now is 05:29 AM.