0

When using redirection of this form 2 > &1, why is there an '&' before 1 but not before 2?

aj31
  • 9
  • You shouldn't think of the & as part of a filename-like thing (i.e. &1 meaning file descriptor 1); it's part of the >& redirection operator. In fact, 2 > &1 will get you an error message because there cannot be a space between > and &, and putting a space between 2 and >& means the 2 is not part of the redirection operator, it's just an unrelated command argument, but you can put a space between >& and 1, so 2>& 1 will work fine. – Gordon Davisson Mar 03 '24 at 06:12
  • It doesn't really answer this question, but see our canonical reference,  What are the shell's control and redirection operators? – G-Man Says 'Reinstate Monica' Mar 03 '24 at 06:42
  • Why would there be? Or, are you wondering why the redirection syntax doesn't have that & on the left side always, even with a filename, e.g. &2>errors.out? Wouldn't that be just an extra nuisance to write over 2>errors.out – ilkkachu Mar 03 '24 at 08:54
  • 1
    I'm voting to close this as a question of why some syntax is the way it is basically a matter of opinion. Of course if, when this feature was originally introduced, the developers had picked whatever other syntax you'd prefer, we might well be seeing questions as to why it's not this instead. – muru Mar 03 '24 at 11:48

1 Answers1

1

The >& denotes a file descriptor duplication operation. 1 and 2 are the actual file descriptor references.

The subject of file descriptor duplication is explained in more detail here (and within that article's references): Redirection using file descriptors - specific example

klode
  • 44