Just trying to redirect stout to stderr. But I'm a little confused about these two commands.

echo test 2>/dev/null 1>&2
echo test 1>&2 2>/dev/null

I don't understand why the second one doesn't work properly.

  • 3
    Because the order of redirections matters.
    – user147505
    Oct 10 '19 at 14:57
  • I expected the first one to be a problem. Are the arguments read from right to left there ?
    – bob dylan
    Oct 10 '19 at 14:58
  • @bobdylan The first one is not a problem, but it's also not the usual way to write it. One ordinarily does >/dev/null 2>&1
    – Kusalananda
    Oct 10 '19 at 14:59
  • 1
    so is echo test 1>&2 2>/dev/null working a little like echo test 1>&2;echo test 2>/dev/null
    – bob dylan
    Oct 10 '19 at 15:01
  • 1
    think of a redirection as of a variable assignment. 2>/dev/null 1>&2 is err:=/dev/null; out:=err, and 1>&2 2>/dev/null is out:=err; err:=/dev/null. After the 1st, both out and err will be /dev/null, and after the 2nd, err will be /dev/null, but out will be whatever err was before the operation (eg. a handle to your tty)
    – mosvy
    Oct 10 '19 at 16:54

Browse other questions tagged or ask your own question.