On 04/17/2016 03:21 AM, Jeremy Nicoll wrote:
On Sun, 17 Apr 2016, at 07:25, Michael Milliman wrote:
Yes, this is the correct incantation. The difference is very subtle.
With ... 2>&1 >/dev/null, the error output is redirected to be the same
as the standard output, and then the standard output is redirected to
/dev/null -- leaving the error output still going to the original
standard output (terminal).
Why is the "2>&1" part needed? Wouldn't stderr go to the terminal by
default?
stderr goes to terminal by default, yes. And in the first, incorrect
incantation ...2>&1 >/dev/null, the redirection of the stderr to the
same as stdout has basically no effect, though it does in theory
redirect the stderr to the same place as stdout. In the second
incantation ...>/dev/null 2>&1 the stdout is first redirected where you
want the ouput to go, then the stderr is sent to the same place as
stdout. This has the effect of redirecting both outputs to /dev/null,
whereas the incorrect syntax leaves the stderr going to the terminal
while stdout is properly redirected this is the same effect as if the
2>&1 portion was not present because it was processed before the
redirection of stdout to /dev/null. Bash processes the redirections in
order as it reads them on the command line, and acts appropriately.
--
Mike