Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

11–20 of 260 posts

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

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

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

#12
A gotcha for me originally and perhaps others is that while using ordering like

   $ ./outerr  >blah 2>&1
sends stdout and stderr to blah, imitating the order with pipe instead does not.

   $ ./outerr  | 2>&1 cat >blah
   err
This is because | is not a mere redirector but a statement terminator.

    (where outerr is the following...)
    echo out 
    echo err >&2

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

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

Yep, there's a strong unifying feel between the Unix api, C, the shell, and also say Perl. Which is lost when using more modern or languages foreign to Unix.

Python too under the hood, a lot of its core is still from how it started as a quick way to do unixy/C things.

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

#14
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 the time, the users were the programmers.

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

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

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

#17
post #14
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 the time, the users were the programmers.

arguably if you're using the CLI they still are

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

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

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

#20
post #2

Not sure why this link and/or question is here, except to say LLMs like this incantation. It redirects STDERR (2) to where STDOUT is piped already (&1). Good for dealing with random CLI tools if you're not a human.

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).
Post reply on HN