4

man time shows it has some options, like this, to write just the time output to a file instead of to stderr:

-o FILE, --output=FILE

So, I try to use it

$ time -o out.txt ls
-o: command not found

real 0m0.081s user 0m0.070s sys 0m0.012s

This does not work! It says -o: command not found.

To prove out.txt doesn't exist:

$ cat out.txt
cat: out.txt: No such file or directory

What am I doing wrong?

How can I use options like -o somefile.txt with time?

I am on Linux Ubuntu 18.04, using bash as my terminal.

thanasisp
  • 8,122
  • Yes it looks like even the example quoted in the manpage fails time -a -o log emacs bork – Roel Van de Paar Apr 21 '22 at 01:01
  • 1
    You have to specify ... what time is it. Use command time or /usr/bin/time see also: https://unix.stackexchange.com/a/10747/216907 – thanasisp Apr 21 '22 at 01:02
  • 1
    Your question probably can be linked here: https://unix.stackexchange.com/questions/86266/make-bash-use-external-time-command-rather-than-shell-built-in – thanasisp Apr 21 '22 at 01:19

1 Answers1

11

The bash shell is evaluating the first word "time" in your command as the reserved word time and is waiting for a command at the second word, which is -o, so it's printing: -o: command not found.

At bash manual we can see the reason time is a reserved word:

The use of time as a reserved word permits the timing of shell builtins, shell functions, and pipelines. An external time command cannot time these easily.

In order to use the time command, and use its useful arguments, you have to instruct the bash shell so. It is better to do it using the command utility:

command time -o out.txt ls

Or you could use the full path:

/usr/bin/time -o out.txt ls

Or even quote it:

"time" -o out.txt ls

Or escape it with a backslash:

\time -o out.txt ls

See also posts: 1 and 2

thanasisp
  • 8,122