Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

101–110 of 260 posts

Re: What does " 2>&1 " mean?

#101
post #26
post #23

Earlier quoted context omitted.

This is misleading because you use plural for both and I'm sure most of these UX missteps were _each_ made by a _single_ person, and there were >1 users even at the time.

I think he meant that at that time all users were programmers. Yes, _all_ .

It was a bit of an over-generalization, but yes that's basically what I was going for.

Re: What does " 2>&1 " mean?

#102
post #56

So if i happen to know the numbers of other file descriptors of the process (listed in /proc), i can redirect to other files opened in the current process? 2>&1234? Or is it restricted to 0/1/2 by the shell? Would probably be hard to guess since the process may not have opened any file once it started.

No restrictions. You can create your own beautiful monsters that way.

> Would probably be hard to guess since the process may not have opened any file once it started.

You need to not only inspect the current state, but also race the process before the assignments change.

Re: What does " 2>&1 " mean?

#103
I regularly refer to [the unix shell specification][1] to remember the specifics of ${foo%%bar} versus ${foo#bar}, ${parameter:+word} versus ${parameter:-word}, and so on.

It also teaches how && and || work, their relation to [output redirection][3] and [command piping][2], [(...) versus {...}][4], and tricky parts like [word expansion][5], even a full grammar. It's not exciting reading, but it's mostly all there, and works on all POSIXy shells, e.g. sh, bash, ksh, dash, ash, zsh.

[1]: https://pubs.opengroup.org/onlinepubs/7908799/xcu/chap2.html

[2]: https://pubs.opengroup.org/onlinepubs/7908799/xcu/chap2.html...

[3]: https://pubs.opengroup.org/onlinepubs/7908799/xcu/chap2.html...

[4]: https://pubs.opengroup.org/onlinepubs/7908799/xcu/chap2.html...

[5]: https://pubs.opengroup.org/onlinepubs/7908799/xcu/chap2.html...

Re: What does " 2>&1 " mean?

#104
post #11

It's a reminder of how archaic the systems we use are. File descriptors are like handing pointers to the users of your software. At least allow us to use names instead of numbers. And sh/bash's syntax is so weird because the programmer at the time thought it was convenient to do it like that. Nobody ever asked a user.

> At least allow us to use names instead of numbers.

You can use /dev/stdin, /dev/stdout, /dev/stderr in most cases, but it's not perfect.

Re: What does " 2>&1 " mean?

#105
post #56

So if i happen to know the numbers of other file descriptors of the process (listed in /proc), i can redirect to other files opened in the current process? 2>&1234? Or is it restricted to 0/1/2 by the shell? Would probably be hard to guess since the process may not have opened any file once it started.

> Or is it restricted to 0/1/2 by the shell?

It is not. You can use any arbitrary numbers provided they're initialized properly. These values are just file descriptors.

For Example -> https://gist.github.com/valarauca/71b99af82ccbb156e0601c5df8...

I've used (see: example) to handle applications that just dump pointless noise into stdout/stderr, which is only useful when the binary crashes/fails. Provided the error is marked by a non-zero return code, this will then correctly display the stdout/stderr (provided there is <64KiB of it).

Re: What does " 2>&1 " mean?

#106
post #11

It's a reminder of how archaic the systems we use are. File descriptors are like handing pointers to the users of your software. At least allow us to use names instead of numbers. And sh/bash's syntax is so weird because the programmer at the time thought it was convenient to do it like that. Nobody ever asked a user.

You can do:

   2>/dev/stdout
Which is about the same as `2>&1` but with a friendlier name for STDOUT. And this way `2> /dev/stdout`, with the space, also works, whereas `2> &1` doesn't which confuses many. But it's behavior isn't exactly the same and might not work in all situations.

And of course I wish you could use a friendlier name for STDERR instead of `2>`

Re: What does " 2>&1 " mean?

#107
post #46
post #23

Earlier quoted context omitted.

This is misleading because you use plural for both and I'm sure most of these UX missteps were _each_ made by a _single_ person, and there were >1 users even at the time.

> and there were >1 users even at the time. Are you sure there wasn't >&1 users... Sorry I'll get my coat.

I think that's likely to work as a no-op

Re: What does " 2>&1 " mean?

#108

Earlier quoted context omitted.

> bash's syntax is so weird What should be the syntax according to contemporary IT people? JSON? YAML? Or just LLM prompt?

There's a movement to write JSON to fd 3, as a machine-parsable alternative to rickety fd 1.

Anything that is infected by UCS-2 / UTF-16 garbage should be revised and reconsidered... Yeah UTF-8 has carve outs for those escape sequences... However JSON is even worse, you _have_ to use UTF-16 escapes. https://en.wikipedia.org/wiki/JSON#Character_encoding

Re: What does " 2>&1 " mean?

#109
post #51

Earlier quoted context omitted.

Another fun consequence of this is that you can initialize otherwise-unset file descriptors this way: $ cat foo.sh #!/usr/bin/env bash >&1 echo "will print on stdout" >&2 echo "will print on stderr" >&3 echo "will print on fd 3" $ ./foo.sh 3>&1 1>/dev/null 2>/dev/null will print on fd 3 It's a trick you can use if you've got a super chatty script or set of scripts, you want to silence or slurp up all of their output,…

Interesting. Is this just literally “fun”, or do you see real world use cases?

The aws cli has a set of porcelain for s3 access (aws s3) and plumbing commands for lower level access to advanced controls (aws s3api). The plumbing command aws s3api get-object doesn't support stdout natively, so if you need it and want to use it in a pipeline (e.g. pv), you would naively do something like

  $ aws s3api get-object --bucket foo --key bar /dev/stdout | pv ...
Unfortunately, aws s3api already prints the API response to stdout, and error messages to stderr, so if you do the above you'll clobber your pipeline with noise, and using /dev/stderr has the same effect on error.

You can, though, do the following:

  $ aws s3api get-object --bucket foo --key bar /dev/fd/3 3>&1 >/dev/null | pv ...
This will pipe only the object contents to stdout, and the API response to /dev/null.

Re: What does " 2>&1 " mean?

#110

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…

Isn't that because of posix?
Post reply on HN