Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

61–70 of 260 posts

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

#61
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.

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

Trying to be language agnostic: it should be as self-explanatory as possible. 2>&1 is all but.

Why is there a 2 on the left, when the numbers are usually on the right. What's the relationship between 2 and 1? Is the 2 for std err? Is that `&` to mean "reference"? The fact you only grok it if you know POSIX sys calls means it's far from self explanatory. And given the proportion of people that know POSIX sys calls among those that use Bash, I think it's a bit of an elitist syntax.

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

#62
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?

Multiple levels of logging, all of which you want to capture but not all in the same place.

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

#63

I enjoyed the commenter asking “Why did they pick such arcane stuff as this?” - I don’t think I touch more arcane stuff than shell, so asking why shell used something that is arcane relative to itself is to me arcane squared.

I love myself a little bit of C++. A good proprietary C++ codebase will remind you that people just want to be wizards, solving their key problem with a little bit of magic.

I've only ever been tricked into working on C++...

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

#65
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.

> 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.

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

#67
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 for the destination. That's the whole reason you need the "&": to tell the shell the destination is not a named file (which itself could be a pipe or socket). And by default you don't need to specify the source fd at all. The intent is that stdout is piped along but stderr goes directly to your tty. That's one reason they are separate.

And for those saying "<" would have been better: that is used to read from the RHS and feed it as input to the LHS so it was taken.

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

#68
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.

It should be a lesson to learn on how simple, logical and reliable tools can last decades.

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

#70
post #17
post #14

Earlier quoted context omitted.

At the time, the users were the programmers.

arguably if you're using the CLI they still are

Yeah but now they're using npm to install a million packages to do things like tell if a number is greater than 10000. The chances of the programmer wanting to understand the underlying system they are using is essentially nil.
Post reply on HN