Linux – Why does the output of some Linux programs go to neither STDOUT nor STDERR

bashgnulinuxstderrstdout

Why does the output of some Linux programs go to neither STDOUT nor STDERR?

Actually, I want to know how to reliably capture all program output, no matter what 'stream' it uses. The problem I have is that some programs do not seem to let their output be captured.

An example is the 'time' command:

time sleep 1 2>&1 > /dev/null

real        0m1.003s
user        0m0.000s
sys         0m0.000s

or

time sleep 1 &> /dev/null

real        0m1.003s
user        0m0.000s
sys         0m0.000s

Why do I see output both times? I expected it all to be piped into /dev/null.

What output stream is time using, and how can I pipe it into a file?

One way to work around the problem is to create a Bash script, for example, combine.sh containing this command:

$@ 2>&1

Then the output of 'time' can be captured in the correct way:

combine.sh time sleep 1 &> /dev/null

(no output is seen – correct)

Is there a way to achieve what I want without using a separate combine script?

Best Answer

This question is addressed in BashFAQ/032. In your example, you would:

{ time sleep 1; } 2> /dev/null

The reason why

time sleep 1 2>/dev/null

doesn't behave how you're expecting is because with that syntax, you'll want to time the command sleep 1 2>/dev/null (yes, the command sleep 1 with stderr redirected to /dev/null). The builtin time works that way so as to make this actually possible.

The bash builtin can actually do this because... well, it's a builtin. Such a behavior would be impossible with the external command time usually located in /usr/bin. Indeed:

$ /usr/bin/time sleep 1 2>/dev/null
$

Now, the answer to your question

Why does the output of some linux programs go to neither STDOUT nor STDERR?

is: it does, the output goes to stdout or stderr.

Hope this helps!

Related Question