How to run "time" on a function in zsh

7

This works:

time ls -l

This does not work

f() { ls -l }
time f

No time output is printed in the second case. Why?

justingordon

Posted 2013-12-13T20:52:32.760

Reputation: 1 073

Answers

4

@John1024 gave you the answer for bash. I try to answer the zsh tag...

You get the timing statistics, if you spawn a subshell for your function:

% zsh
% f() { sleep 1 }
% time f
% time (f)
( f; )  0.00s user 0.05s system 4% cpu 1.061 total
% time sleep 1
sleep 1  0.00s user 0.03s system 2% cpu 1.045 total

This adds a little overhead, but as you can see from this (non-faked ;)) example, it's probably insignificant.

mpy

Posted 2013-12-13T20:52:32.760

Reputation: 20 866

4

You have tagged this with both bash and zsh. This answer applies to bash.

This is an error in bash:

f() { ls -l }

What works instead is:

f() { ls -l ; }

With that new definition of f, the time command works.

Under bash, when grouping commands together in braces, the last command must be followed by a semicolon or a newline. This is documented under "Compound Commands" in man bash.

(I would test this solution under zsh but I don't currently have it installed. But, as per this SO post, the solution under zsh might be to run f in a subshell. Update: See @mpy's answer for zsh.)

John1024

Posted 2013-12-13T20:52:32.760

Reputation: 13 893

1Even with the semicolon, you don't get a time report in zsh. – mpy – 2013-12-13T21:13:23.593