Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

211–220 of 260 posts

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

#211
post #4

I find it easier to understand in terms of the Unix syscall API. `2>&1` literally translates as `dup2(1, 2)`, and indeed that's exactly how it works. In the classic unix shells that's all that happens; in more modern shells there may be some additional internal bookkeeping to remember state. Understanding it as dup2 means it's easier to understand how successive redirections work, though you also have to know that re…

> The pipe operator works similarly, though it's a combination of fork and dup'ing

Any time the shell executes a program it forks, not just for redirections. Redirections will use dup before exec on the child process. Piping will be two forks and obviously the `pipe` syscall, with one process having its stdout dup'd to the input end of the pipe, and the other process having its stdin dup'd to the output end.

Honestly, I find the BASH manual to be excellently written, and it's probably available on your system even without an internet connection. I'd always go there than rely on stack overflow or an LLM.

https://www.gnu.org/software/bash/manual/bash.html#Redirecti...

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

#212

Sometime all you need is to RTMF from the source instead of Nth hand information (N > 1) https://www.gnu.org/software/bash/manual/html_node/Redirecti...

Great if you know where to look, but most people who ask themselves the question don't know they have to look up the bash manual in the "redirection" section.

The usual thing (before LLMs) is to Google the question, but for the question to appear in Google, someone has to ask it first, and here we are.

Also the Stackoverflow answers give different perspectives, context, etc... rather than just telling you what it does, which is useful to someone unfamiliar with how redirections work. As I said, someone who doesn't know about "2>&1" is unlikely to be an expert given how common the pattern is, so a little hand holding doesn't hurt.

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

#213
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

I don't have macos right now but I think that it doesn't have these files. What's worse is that bash emulates these files so they might even somewhat work, but not in all situations. I distinctly remember issues with this command:

    install /dev/stdin file 

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

#214

Claude’s answer, which is the only one that clicked for me: Normally when you do something like command > file.txt, you’re only capturing the normal output — errors still go to your screen. 2>&1 is how you say: “send the error pipe into the same place as the normal output pipe.” Breaking it down without jargon: • 2 means “the error output” • > means “send it to” • &1 means “wherever the normal output is currently goi…

> Claude’s answer This response is essentially just the second answer to the linked question (the response by dbr) with a bunch of the important words taken out. And all it cost you to get it was more water and electricity than simply clicking the link and scrolling down — to say nothing of the other costs.

FWIW, I clicked the link, scanned the SO thread, then scanned the HN thread. The "bunch of important words taken out" is exactly the service I paid AI for.

"I didn't have time to write you a short letter, so I wrote you a long one." is real.

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

#216
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…

There must be a law of system design about this, because this happens all the time. Every abstraction creates a class of users who are powerful but fragile. People who build a system or at least know how it works internally want to simplify their life by building abstractions. As people come later to use the system with the embedded abstractions, they only know the abstractions but have no idea of the underlying impl…

> Those abstractions used to make perfect sense for those with prior knowledge but can also carry subtle bias which makes their use error prone for non initiated users.

I don't think 2>&1 ever made any sense.

I think shell language is simply awful.

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

#217

Sometime all you need is to RTMF from the source instead of Nth hand information (N > 1) https://www.gnu.org/software/bash/manual/html_node/Redirecti...

And how do you find that? Google search literally is useless for these days, for Average Joe.

Do these days include the nearly seventeen years ago when this question was posted? ;)

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

#218

Earlier quoted context omitted.

And how do you find that? Google search literally is useless for these days, for Average Joe.

Do these days include the nearly seventeen years ago when this question was posted? ;)

https://www.gnu.org was there for decades, much older than SO and google.

I know GNU -> Google -> SO -> LLM is a culture shift, this is how human attention goes. The search engine and LLM only capture the latest group attention and we have short memory. That is why reading is a critical skill for us as human beings, we can't afford to outsource that to machine. (Same with writing.)

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

#219

Sometime all you need is to RTMF from the source instead of Nth hand information (N > 1) https://www.gnu.org/software/bash/manual/html_node/Redirecti...

And how do you find that? Google search literally is useless for these days, for Average Joe.

Try "info bash" on your system. It's the same manual.

In Emacs, when I hit C-h i I get a menu of all my info manuals and I first read the bash one there.

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

#220
post #196

Earlier quoted context omitted.

> A Stack Overflow question matching my problem cleared that up for me. Perhaps if there was no question already available you'd have had a different experience. Getting clearly written and specific questions promptly closed as duplicates of related, yet distinct issues, was part of the fun. I find that AI hallucinates in the same way that someone can be very confident and wrong at the same time, with the difference…

> someone can be very confident and wrong at the same time And sometimes that someone can be you, and AI is notoriously bad at telling you that you're wrong (because it has to please people)

I've found recent claude code to be surprisingly good at dispelling false assumptions and incorrect framing. I say this as someone who experimented with it last summer and found it to be kinda stupid; since December last year it's turned the curve - it's not the sycophantic nonsense it used to be.
Post reply on HN