LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 01-18-2009, 10:04 AM   #1
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
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?
 
Old 01-18-2009, 11:42 AM   #2
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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

Last edited by b0uncer; 01-18-2009 at 11:48 AM.
 
Old 01-18-2009, 11:46 AM   #3
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Original Poster
Rep: Reputation: 15
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.
 
Old 01-18-2009, 12:56 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Quote:
Originally Posted by exscape View Post
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.
 
Old 01-18-2009, 12:57 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
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
 
Old 01-19-2009, 06:25 AM   #6
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Original Poster
Rep: Reputation: 15
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().
 
Old 01-19-2009, 07:01 AM   #7
pastipet
LQ Newbie
 
Registered: Jan 2007
Location: Helsinki, Finland
Distribution: OpenSuSE 11.1, 11.2
Posts: 4

Rep: Reputation: 0
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
 
Old 01-19-2009, 07:06 AM   #8
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pastipet View Post
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.
 
Old 04-09-2011, 09:55 PM   #9
Mihai Cilidariu
LQ Newbie
 
Registered: Apr 2011
Posts: 1

Rep: Reputation: 0
$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.
 
Old 07-23-2012, 05:03 AM   #10
damko
LQ Newbie
 
Registered: Oct 2008
Location: Como - Italy
Distribution: mint
Posts: 4

Rep: Reputation: 6
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!
 
1 members found this post helpful.
  


Reply

Tags
bash, prompt, ps1, time


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting time and time zone altogether properly in Slack-current, KDE4 spaceballs Slackware 3 04-20-2011 05:15 AM
how to display the current time at a different timezone stinkytofoo Linux - Software 3 07-18-2007 03:41 AM
Bash Prompt - display correct $(pwd) tschima Linux - General 2 07-05-2007 07:03 PM
how do i change the current prompt in shell? prospekrisal Linux - Newbie 4 07-16-2006 10:35 AM
display time in the prompt rpb Linux - General 1 05-18-2005 04:03 PM


All times are GMT -5. The time now is 01:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration