Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `time` not work on `echo`?

Tags:

time

echo

zsh

time seems to work generally on my system:

āžœ  ~ time touch tmp.txt
touch tmp.txt  0.00s user 0.00s system 54% cpu 0.007 total

Yet its output seems to get suppressed when I attempt to time echo.

āžœ  ~ time echo šŸ”
šŸ”

And to thicken the plot: it seems to recover its ability to measure echo, provided I throw that echo into a pipeline:

āžœ  ~ time echo šŸ” | xxd     
00000000: f09f 8d94 0a                             .....
echo šŸ”  0.00s user 0.00s system 38% cpu 0.002 total
xxd  0.00s user 0.00s system 88% cpu 0.003 total

What's going on? Is echo outputting to time's favourite stream, replacing its report?

Bonus points for: is there some way (e.g. stream redirection) to make time echo šŸ” output the usual report?


Edit: Some commenters have pointed out that I am using zsh. I'd not noticed that at the time, but that explains a lot.

bash uses normal executables to do time and echo.

bash-3.2$ which echo
/usr/local/opt/coreutils/libexec/gnubin/echo
bash-3.2$ which time
/usr/bin/time

(Looks like I am using a GNU version of echo rather than the presumably BSD one, /bin/echo, that comes with Mac OS X).

Whereas zsh provides something significantly more special:

āžœ  ~ which echo
echo: shell built-in command
āžœ  ~ which time
time: shell reserved word

Neither of these are normal executables. Very interesting!

like image 610
Birchlabs Avatar asked Nov 21 '25 09:11

Birchlabs


1 Answers

This seems to happen when you use the built-in time (which measures a complete pipeline) combined with another built-in command, like echo, true.

Curiously, I could not find any mention of this in the zshall man page.

To work around it, run this:

time command echo šŸ”

The command precommand modifier invokes the external echo instead of the built-in one.

like image 60
Roland Illig Avatar answered Nov 24 '25 17:11

Roland Illig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!