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.

3

1 Answer 1

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

1
  • 1
    This is quite informative. Thank you for providing multiple solutions. Dec 3, 2023 at 9:37

Not the answer you're looking for? Browse other questions tagged .