Skip to content
Advertisement

Grep time command output

Using time ls, I have the following output:

JavaScript

Now, when I try to grep only the real value line, the actual result is:

JavaScript

My question is, how to get only the real value as output? In this case, 0m0.040s.

Advertisement

Answer

time writes its output to stderr, so you need to pipe stderr instead of stdout. But it’s also important to remember that time is part of the syntax of bash, and it times an entire pipeline. Consequently, you need to wrap the pipeline in braces, or run it in a subshell:

JavaScript

With Bash v4.0 (probably universal on Linux distributions but still not standard on Mac OS X), you can use |& to pipe both stdout and stderr:

JavaScript

Alternatively, you can use the time utility, which allows control of the output format. On my system, that utility is found in /usr/bin/time:

JavaScript

man time for more details on the time utility.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement