You are not logged in.
To show timestamp in command prompt at terminal startup.
something like in ~/.bashrc
PS1='[\u@\h \W] \T $ '
just Enter to update that timestamp.
but how can I automatically update that timestamp?
I tried like this but not work.
PS1='[\u@\h \W] $(while true; do \T sleep 1 done &) $ '
any idea?
Last edited by duyinthee (2016-01-29 08:14:28)
Offline
Are you trying to have the time update on your current prompt? I don't know if that's even possible ... or wise.
Offline
I don't think bash can do that. There is PROMPT_COMMAND, but that's only for setting a function that is executed whenever the prompt is (newly) displayed.
But zsh can do it - I found this zsh config snippet in my dotfiles' git history:
setopt prompt_subst
TMOUT=1
TRAPALRM() {
zle reset-prompt
}
PROMPT='%D{%H:%M:%S}'
However, I agree with Ziusudra: it's not very wise.
There is a reason why it's in my dotfile's git history: after the initial excitement about a clock in the prompt (yay?), I started getting annoyed about the used space and the occasional glitches (especially at terminal resize events). Also, it feels a little silly, given that my WM is perfectly able to display the time on its own.
If, for some reason, you really need a clock in your shell, have you taken a look at tmux? It is capable of displaying the current time in the status bar, which keeps the amount of stuff in your shell prompt to a minimum (on the other hand, it's a terminal multiplexer, which comes with its own set of benefits/problems).
Last edited by ayekat (2016-01-29 09:49:18)
Offline
after the initial excitement about a clock in the prompt (yay?), I started getting annoyed about the used space and the occasional glitches (especially at terminal resize events). Also, it feels a little silly, given that my WM is perfectly able to display the time on its own.
I love having a timestamp in my prompt! It gives me a general idea of when a command was run and how long it took to finish. I kind of wish there was a way to get that information into my Bash history too! I've never had it glitch. And I use the extra space it takes up to easily visually separate the commands I type:
8:15:24 am drcouzelis@archsystem
[ Documents ]$ cat blarg.txt
Blarg.
Blarg?
BLARG!
8:15:44 am drcouzelis@archsystem
[ Documents ]$
Just posting another opinion on the matter.
Offline
That isn't the same as the prompt dynamically updating, though; which seems to be what the OP is after. And given the utility of that would only be if you sat staring at a blank terminal...
Offline
That isn't the same as the prompt dynamically updating, though; which seems to be what the OP is after. And given the utility of that would only be if you sat staring at a blank terminal...
That's true. On a mildly related note, I wonder if it's time for me to try switching to zsh...
Offline
@drcouzelis, I do have configured zsh to display the running time of the last command in the RPROMPT if the time > 1 second (so it wouldn't pop up for a simple `ls` or `cd`):
preexec() {
timer=${timer:-$SECONDS}
}
precmd() {
RPROMPT=''
if [ -n "$timer" ]; then
seconds=$(($SECONDS - $timer))
if [ $seconds -gt 1 ]; then
RPROMPT="$seconds"
fi
unset timer
unset seconds
fi
export RPROMPT
}
setopt promptsubst
precmd
Of course this was the thing I did right after I realised a clock in the prompt is overkill for my needs, whereas this does exactly what I want: give me an idea of how long commands take to run. With a little more accurate styling I get something like this:
On a mildly related note, I wonder if it's time for me to try switching to zsh...
Yes
Offline
@ayekat: Does it update while the command is running, or just print after the command completes?
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
Actually, it is useful to have update time and battery status at current prompt when we working with awfully minimalist WMs like EvilWM.
Bash can't do that but bash can do $(acpi) at least. So, at last I do that:
PS1='\n\D {%F %T} | $(acpi)\n[\u@\h \W] \$ '
just Enter to update it.
It is useful for me.
Thanks for replies.
Offline
@alphaniner, It just prints the running time of the last executed command in the next prompt. So yes, I can't see the running time during the execution.
However that doesn't bother me too much. I can run `ps` to see the start time of a running process (which is not possible for already completed processes, hence this prompt).
@duyinthee, I would definitely suggest using tmux.
Offline