27

I've just switched to zsh. However, I really don't like how the time builtin command also outputs the command that it's timing. I much prefer the bash style output. Anyone know how to switch it over?

Zsh:

[casqa1:~/temp]$ time grep foo /dev/null
/usr/local/gnu/bin/grep --color -i foo /dev/null  0.00s user 0.00s system 53% cpu 0.004 total

Bash:

[casqa1:~/temp]$ bash
casqa1.nyc:~/temp> time grep foo /dev/null

real        0.0
user        0.0
sys         0.0

Thanks,

/YGA

Doug Harris
  • 27,333
  • 17
  • 78
  • 105
YGA
  • 1,881
  • 8
  • 24
  • 23

3 Answers3

36

This is fairly close:

$ TIMEFMT=$'\nreal\t%*E\nuser\t%*U\nsys\t%*S'

$ time sleep 1

real    1.01s
user    0.00s
sys     0.00s
Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
  • 8
    My proposed edit was rejected 2-1. Ideally, the format should be `TIMEFMT=$'\nreal\t%*E\nuser\t%*U\nsys\t%*S'`. The `*`s in the flags formats them as hours/minutes as per bash, instead of just seconds. More information on `TIMEFMT` [here](http://zsh.sourceforge.net/Doc/Release/Parameters.html#index-TIMEFMT). – Sparhawk May 16 '16 at 07:49
  • I'm trying to get colours with something like: `export TIMEFMT="%J \033[1;32m%U\033[0m user \e[1;32m%S\e[0m system \e[1;32m%P\e[0m cpu \e[1;32m%*E\e[0m total"`, but it is just printing the raw string instead of colours. Is this possible? – Sahas Nov 07 '20 at 12:08
  • Use a "C-quoted string": `TIMEFMT=$'%J \033[1;32m%U\033[0m user \e[1;32m%S\e[0m system \e[1;32m%P\e[0m cpu \e[1;32m%*E\e[0m total'` (do you really need to export it?). Cool idea, btw! – Dennis Williamson Jun 28 '22 at 13:31
  • 1
    @Sparhawk: I changed my answer to use your suggestion. Thanks! – Dennis Williamson Jun 28 '22 at 13:36
11

Another option is to disable the builtin command and use the time binary provided by your operating system. I have the following in my .zshrc:

disable -r time       # disable shell reserved word
alias time='time -p ' # -p for POSIX output

This way time outputs to STDERR.

exic
  • 469
  • 5
  • 9
5

Just a small precision regarding Dennis Williamson's very useful answer (the "fairly close" part): bash's built-in time outputs to stderr, while zsh's outputs to stdout.

This command can illustrate the difference: time (echo abc) 2>/dev/null

In bash, it outputs:

    $ time (echo abc) 2>/dev/null
    abc

In zsh, with the suggested TIMEFMT variable:

    $ time (echo abc) 2>/dev/null
    abc

    real    0.00s
    user    0.00s
    sys     0.00s
anol
  • 1,731
  • 4
  • 22
  • 35