logoalt Hacker News

raincoletoday at 2:37 AM4 repliesview on HN

The comments on stackoverflow say the words out of my mouth so I'll just copy & paste here:

> but then shouldn't it rather be &2>&1?

> & is only interpreted to mean "file descriptor" in the context of redirections. Writing command &2>& is parsed as command & and 2>&1

That's where all the confusion comes from. I believe most people can intuitively understand > is redirection, but the asymmetrical use of & throws them off.

Interestingly, Powershell also uses 2>&1. Given an once-a-lifetime chance to redesign shell, out of all the Unix relics, they chose to keep (borrow) this.


Replies

ptxtoday at 9:52 AM

Although PowerShell borrows the syntax, it (as usual!) completely screws up the semantics. The examples in the docs [1] show first setting descriptor 2 to descriptor 1 and then setting descriptor 1 to a newly opened file, which of course is backwards and doesn't give the intended result in Unix; e.g. their example 1:

  dir C:\, fakepath 2>&1 > .\dir.log
Also, according to the same docs, the operators "now preserve the byte-stream data when redirecting output from a native command" starting with PowerShell 7.4, i.e. they presumably corrupted data in all previous versions, including version 5.1 that is still bundled with Windows. And it apparently still does so, mysteriously, "when redirecting stderr output to stdout".

[1] https://learn.microsoft.com/en-us/powershell/module/microsof...

jcotton42today at 8:43 AM

PowerShell actually has 7 streams. Success, Error, Warning, Verbose, Debug, Information, and Progress (though Progress doesn't get a number) https://learn.microsoft.com/en-us/powershell/module/microsof...

xeyownttoday at 8:56 AM

I don't get the confusion.

You redirect stdout with ">" and stderr with "2>" (a two-letter operator).

If you want to redirect to stdout / stderr, you use "&1" or "&2" instead of putting a file name.

zwischenzugtoday at 4:28 AM

Isn't that because of posix?

show 1 reply