0

Are the redirections 3>&2 and 3<&2 different?

What are the differences?

Is it only that in 3>&2 should raise an error as 2 is not open for input?

Compare:

{ seq 20 1<&2; } 2>/dev/null

{ seq 20 1>&2; } 2>/dev/null

EDIT: answer

Both the commands execute the same duplication of 1 and 2. The output of this:

$ strace -f -e trace=dup,dup2 -o o2 sh -c 'seq 20 1>&2'

is exactly equal (beside PID numbers) for both commands.

  • See also What does “sh a.sh <&0 >&0” mean? – Stéphane Chazelas Jul 25 '18 at 13:01
  • @StéphaneChazelas I already understood that <&0 affects stdin only and that >&0 affects stdout, but thanks anyway. – Isaac Jul 25 '18 at 13:12
  • Why the downvote? What is wrong in asking a question that has never been asked before? – Isaac Jul 25 '18 at 16:37
  • no idea. Not mine. – Stéphane Chazelas Jul 25 '18 at 16:44
  • No @StéphaneChazelas it was not directed to you, sorry for the noise. I just hope that the comment could be directed to the user down voting. In any case, please, you never mind, thanks. – Isaac Jul 25 '18 at 16:47
3

No, they're strictly equivalent, the shell ends up doing dup2(2, 3) for both 3>&2 and 3<&2. The fd 3 then ends up pointing to the same open file description as on fd 2. It has no influence on the mode/direction of that open file description. It is not possible to change the mode/direction of an open file description.

The x>&y and x<&y operators are only different when x is omitted. >&y is 1>&y (same as 1<&y) while <&y is 0<&y (same as 0>&y).

  • This is what I already mentioned: the final redirection creates a writable file descriptor. – schily Jul 25 '18 at 12:47
2

One redirects stderr for output and the other for input.

If you like to have stderr open for reading and writing, you could use:

2<>/dev/null

If you log in, all three file descriptors are open for reading and writing.

This is done by first opening the tty and then calling dup() 2 times.

The commands

{ seq 20 1<&2; } 2>/dev/null

and

{ seq 20 1>&2; } 2>/dev/null

are equivalent because the final redirection creates a file descriptor that is open for writing.

If you call:

{ truss -o o seq 20 1<&2; } 2>/dev/null

you will see that the write call from seq succeeds, And BTW: the output from seq goes to /dev/null

  • What? You’re answering a question about non-standard behaviour? ;-) – Stephen Kitt Jul 25 '18 at 12:21
  • 1
    Well, <> is in the Bourne Shell since at least 35 years ;-) See POSIX 2.7.7 – schily Jul 25 '18 at 12:23
  • Re-read the question (it changed). Why both { seq 20 1<&2; } 2>/dev/null and { seq 20 1>&2; } 2>/dev/null seem to do the same ? – Isaac Jul 25 '18 at 12:38
  • 1
    There's no such thing as a 3<>&2 operator. The x>&y and x<&y only duplicate file descriptors (get a new fd to the same open file description), they have no influence on the mode of that open file description. – Stéphane Chazelas Jul 25 '18 at 12:49
  • 1
    <> was in Bourne shell from the start (not documented) but was initially broken. Fixed first in Solaris 2.6 (20 years ago) according to in-ulm.de/~mascheck/bourne – Stéphane Chazelas Jul 25 '18 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.