Solved

How to include current time in the command prompt?

Posted on 2000-04-18
13
359 Views
Last Modified: 2013-11-18
Is there any way to include current time in the command prompt?
PS1="["`date +%X`"]" doesn't work.
0
Comment
Question by:kslzzg
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 15

Expert Comment

by:samri
ID: 2730091
It really depends on what shell you are in

in bash (/bin/sh)

PS1=`date "+%X"`

 wii do.

Samri

0
 
LVL 15

Expert Comment

by:samri
ID: 2730099
kslzzq,

Sorry, the above does' answer your qquestion.

Maybe try this one.

In bash (/bin/sh)

PS1=\[`date "+%X"`\]


0
 

Expert Comment

by:shiqi
ID: 2732667
It  can display the time but cannot keep going. The time format is HH:MM:SS
 which means the seconds "SS" should be able to go to keep it current.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 15

Accepted Solution

by:
samri earned 30 total points
ID: 2733165
kstzzq,

I'm not sure how you could do this, but a trick that I can think of now is to create a small program to act as a subshell.

maybe it defeats a lot pf purposes (ENV vars for example). Anyway it shoudl get you started.

save the file as let say "cmd.pl", chmod +x cmd.pl, and run it. So every time you logged it, run the file, and voila, you got the prompt that tells you the time.

I would however, recommend you to buy a small desktop clock (if your intention is to keep track of time) :)
--
#!/usr/local/bin/perl
$|;
while (1) {
  chop($prompt=`date "+%X"`);
  print "[$prompt] ";
  (@tmplist)=split(/ +/,<STDIN>);
  $cmd = join(" ",@tmplist);
  system(@tmplist);
}          
0
 

Author Comment

by:kslzzg
ID: 2733491
You provided a very good trick. It will be better if the time displayed in the prompt can be automatically refreshed itself even
though I do not press <Enter>, just like a
digital clock.

The reason why I posted this question is totally out of curiosity and you may know that on MS-DOS, I can just run "set prompt=[$t]"  and I will get what your program can do.

Anyway, this is the best alternative solution
that I have ever seen.
0
 
LVL 15

Expert Comment

by:samri
ID: 2734953
kslzzq,

well, cheap -and-dirty-trick :)  Anyway, just some drawback, I believe that you are limited to the environment variable set prior to the program running, ie, if you try to set envrontment variable to something withing the "fake" shell, you can't.  Well I think you can, but need to do some research on those.

Reagarding your intention to get the prompt to be "alive" (ie. the clock is constantly updating) - very intersting.  Unfortunately, with a straght-forward shell-trick, you can't.  maybe with some programming with curses-library, you can actually, get the current cursor position, and update the clock, and refresh the view.  Possible, YES.  How easy, I don't know.  Perhaps there are gurus out there that help on such thing.  But for the purpose of seeing the "live-clock" on the comand prompt, I would abandon the idea.

good luck,
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2761699
ok no live-clock, but in addidition to samri's suggestion, try following variables for the corresponding prompt variable:

  CMD.EXE: $T
  bast: \t
  lsh:  $T
  tcsh: %t %T %p %P %@
  zsh: %t %T %*

actually I don't have a zsh, but IIRC %* is what you're looking for
0
 

Expert Comment

by:rkuethe
ID: 2786080
This is not a 'real-time' solution but anytime your prompt is refreshed, the current time will show (this is in KSH):

export PS1=`'date +%H:%M:%S">"'`

As far as a real-time update I would think you would need a separate process that runs in the bg, captures the time, and "writes" to your tty.

My $.02
0
 

Author Comment

by:kslzzg
ID: 2786868
rkuethe's solution cannot be accepted because it doesn't work on my ksh.
I would prefer samri's solution.
0
 
LVL 15

Expert Comment

by:samri
ID: 2786946
kslzzg,

  Thanks fot the pts. Anyway, the question really pique my curiositu.  What is the purpose of having a "life" clock in the prompt anyway?

-samri
0
 

Expert Comment

by:shiqi
ID: 2787557
I am actually out of curiorsity and also because sometimes I need to find out how long my commands take to finish  but I often forget to key in "date" beforehand and afterwards.  
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2810672
> because sometimes I need to find out how long my commands take to
simply use:
     time command

;-)
0
 

Author Comment

by:kslzzg
ID: 2815554
Sometimes you just forget to time the command,  another problem is I doubt  how can we time a shell script containing many commands and if statements.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Having Returning  Anonymous Function Access Global Function 7 91
Element alignment and word wrapping 9 68
Adjust the position 3 60
Fulfillment API php code sample 1 40
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question