I'm using Mac, and I'm trying to time the command execution.

If I do

time echo

it doesn't have any output

But If I do

time ls

it does give me the output of time function

Any idea why that happens?

Update: turns out it's cuz I'm using zsh, with oh-my-zsh installed. It works well in bash, but no output in zsh. Any idea why?

  • I don't think oh-my-zsh is causing it. I get the same result using zsh without that installed. I do see time results if I do /bin/zsh -c '/usr/bin/time echo' though. – jesse_b Mar 1 '18 at 3:34
  • zsh will happily time anything in parentheses, including builtins and functions. – Michael Homer Mar 1 '18 at 4:00

In zsh, the time keyword has no effect on builtins (or other similar shell-internal constructs). From this mailing list post:

Additional note: The time builtin applied to any construct that is executed in the current shell, is silently ignored. So although it's syntactically OK to put an opening curly or a repeat-loop or the like immediately after the time keyword, you'll get no timing statistics. You have to use parens instead, to force a subshell, which is then timed.

$ time echo

$ time (echo)

( echo; )  0.00s user 0.00s system 51% cpu 0.001 total

The time command will spawn the command echo. Under zsh this is built into the shell so there isn't an external binary being run. Under bash, echo will run the echo binary located in /bin/echo.

So in summary zsh has echo built into itself, bash will spawn a process from the echo binary.

  • it's the same for some other command.. like time rails runner "puts 'hello'" would also give me no time – songyy Mar 1 '18 at 3:46
  • 1
    The built-in-ness of echo does not matter: bash also has a built-in echo and time echo produces time output. There is also an echo binary, but you can try the following experiment: rename /bin/echo or /usr/bin/echo so that it cannot be executed as "echo" by bash - then try time echo so that the built-in is run: you still get time output. – NickD Mar 1 '18 at 3:55
  • @Nick There is no need to rename /bin/echo. Running type echo will tell you that bash will use the built-in when running echo. Built-ins always take precedence over external commands. – Adaephon Mar 1 '18 at 8:29
  • @Adaephon: yes, I know. I was just trying to show conclusively that when time runs it, it is indeed the built-in that is run and not somehow the external command. – NickD Mar 1 '18 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.