Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

241–250 of 260 posts

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

#241
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 Many people probably think in terms of "fd 0" and "fd 1" instead of "standard in" and "standard out", but should you wish to use names at least on modern Linux/BSD systems do: echo message >/dev/stdout echo error_message >/dev/stderr

Bash and zsh also allow, and modern Borne-compatible shells (sh) might too:

   echo >&2 error_message
On Linux, /dev/std* requires the kernel to do file name resolution in the virtual file system because it could point to something nonstandard that isn't a symlink to something like /proc/self/fd/XX and then the kernel has to check that that should hopefully point to a special character device.

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

#242
This is why I dislike sites like stackoverflow. If I needed a quick lookup the v7 manpage explains it better, the v6 doesn't have it, but that's because unix didn't have bourne shell til V7

https://man.cat-v.org/unix_7th/1/sh#:~:text=%3C%26digit%0A%2...

Seriously when it comes to unix RTFM RTFM RTFM and you'll get the top comment on SO and HN rolled into one.

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

#243
post #134

Earlier quoted context omitted.

This is probably one of the reasons why many find POSIX shell languages to be unpleasant. There are too many syntactical sugars that abstract too much of the underlying mechanisms away, to the level that we don't get it unless someone explains it. Compare this with Lisps, for example. There may be only one branching construct or a looping construct. Yet, they provide more options than regular programming languages us…

? (defun even(num) (= (mod num 2) 0)) ? (filter '(6 4 3 5 2) #'even) I'm zero Lisp expert and I don't feel comfortable at all reading this snippet.

This:

> I'm zero Lisp expert

and this:

> I don't feel comfortable at all reading this snippet

are related. The comfort in reading Lisp comes from how few syntactic/semantic rules there are. There's a standard form and a few special forms. Compare that to C - possibly one of the smallest popular languages around. How many syntactical and semantic rules do you need to know to be a half decent C programmer?

If you look at the Lisp code, it has just 2 main features - a tree in the form of nested lists and some operations in prefix notation. It needs some getting used to for regular programmers. But it's said that programming newbies learn Lisps faster than regular programming languages, due to the fewer rules they have to remember.

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

#244

Earlier quoted context omitted.

It’s not like someone woke up one morning and decided to design a confusing language full of shortcuts to make your life harder. Bash is the sum of decades of decisions made, some with poor planning, many contradictory, by hundreds of individuals working all over the world in different decades, to add features to solve and work around real world problems, keep backwards compatibility with decades of working programs,…

Yeah, seriously. It's as if people want to playact as illiterate programmers. The "Redirections" section of the manual [0] is just seven US Letter pages. This guy's cheat sheet [1] that took me ten seconds to find is a single printed page. [0] https://www.gnu.org/software/bash/manual/html_node/Redirecti... > [1] https://catonmat.net/ftp/bash-redirections-cheat-sheet.pdf >

> The "Redirections" section of the manual [0] is just seven US Letter pages.

"Just" seven US Letter pages? You're talking about redirections alone, right? How many such features exist in Bash? I find Python, Perl and even Lisps easier to understand. Some of those languages wouldn't have been even conceived if shell languages were good enough.

There is another shell language called 'execline' (to be precise, it's a replacement for a shell). The redirections in its commands are done using a program named 'fdmove' [1]. It doesn't leave any confusion as to what it's actually doing. fdmove doesn't mention the fact that it resorts to FD inheritance to achieve this. However, the entire 'shell' is based on chain loading of programs (fork, exec, FD inheritance, environment inheritance, etc). So fdmove's behavior doesn't really create any confusion to begin with. Despite execline needing some clever thinking from the coder, I find it easier to understand what it's actually doing, compared to bash. This is where bash and other POSIX shell languages went wrong with abstractions. They got carried away with them.

[1] https://www.skarnet.org/software/execline/fdmove.html

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

#246
post #222
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,…

With exec you can open file descriptors of your current process. if [[ ! -e /proc/$$/fd/3 ]]; then # check if fd 3 already open and if not open, open it to /dev/null exec 3>/dev/null fi >&3 echo "will print on fd 3" This will fix the error you are describing while keeping the functionality intact. Now with that exec trick the fun only gets started. Because you can redirect to subshells and subshells inherit their red…

Nice! Not really sure the point since AI can bang out a much more maintainable (and sync'd) wrapper in go in about 0.3 seconds

(if runners have sh then they might as well have a real compiler scratch > debian > alpine , "don't debug in prod")

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

#248

Earlier quoted context omitted.

Bash syntax is anything but simple or logical. Just look at the insane if-statement syntax. Or how the choice of quotes fundamentally changes behavior. Argument parsing, looping, the list goes on.

You could make a list of WTFs about any language. Bash syntax is the pinnacle of Chesterton's Fence. If you can't articulate why it was done that way, you have no right to remove it. Python would be an absolutely unusable shell language.

I didn't say that there wasn't a reason. I said it was absolute trash to use. It's so bad that the moment I need even the slightest bit of complexity, I will switch away from bash. Can't really say that for any other language.

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

#249
post #244

Earlier quoted context omitted.

Yeah, seriously. It's as if people want to playact as illiterate programmers. The "Redirections" section of the manual [0] is just seven US Letter pages. This guy's cheat sheet [1] that took me ten seconds to find is a single printed page. [0] https://www.gnu.org/software/bash/manual/html_node/Redirecti... > [1] https://catonmat.net/ftp/bash-redirections-cheat-sheet.pdf >

> The "Redirections" section of the manual [0] is just seven US Letter pages. "Just" seven US Letter pages? You're talking about redirections alone, right? How many such features exist in Bash? I find Python, Perl and even Lisps easier to understand. Some of those languages wouldn't have been even conceived if shell languages were good enough. There is another shell language called 'execline' (to be precise, it's a r…

> "Just" seven US Letter pages?

Yes. It's the syntax alongside prose explaining the behavior in detail. Go give it a read.

If you want documentation that's done up in the "modern" style, then you'll prefer that one-page cheat sheet that that guy made. I find that "modern" documentation tends to leave it up to each reader to discover the non-obvious parts of the behavior for themselves.

> I find Python ... easier to understand.

Have you read the [0] docs for Python's 'subprocess' library? The [1] docs for Python's 'multiprocess' library? Or many of the other libraries in the Python standard library that deal with nontrivial process and I/O management? Unless you want to underdocument and leave important parts of the behavior for users to incorrectly guess, such documentation is going to be much larger than a cheat sheet would be.

[0] ...twenty-five pages of...

[1] ...fifty-nine pages of...

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

#250

Earlier quoted context omitted.

Humans used this combination extensively for decades too. I'm no aware of any other simple way to grep both stdout and stderr from a process. (grep, or save to file, or pipe in any other way).

"not humans" are using this extensively precisely because humans used this combination extensively for decades. It's muscle-memory for me. And so is it for LLMs.

>It's muscle-memory for me. And so is it for LLMs.

LLMs have neither muscles nor memories. They're token combinators based on statistical correlation, no more, no less.

That's not to say LLMs can't be useful when they string together tokens. Quite the contrary, in fact. But let's not pretend LLMs are something they're not.

Post reply on HN