Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

31–40 of 260 posts

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

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

I've also found llms seem to love it when calling out to tools, I suppose for them having stderr interspersed messaged in their input doesn't make much difference

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

#33
post #23
post #14

Earlier quoted context omitted.

At the time, the users were the programmers.

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.

programmers are people too! bash syntax just sucks

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

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

I found the explanation useful, about "why" it is that way. I didn't realize the & before the 1 means to tell it is the filedescriptor 1 and not a file named 1.

It's an operator called ">&", the 1 is the parameter.

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

#35
post #30

> I am thinking that they are using & like it is used in c style programming languages. As a pointer address-of operator. [...] 2>&1 would represent 'direct file 2 to the address of file 1'. I had never made the connection of the & symbol in this context. I think I never really understood the operation before, treating it just as a magic incantation but reading this just made it click for me.

No, the shell author needed some way to distinguish file descriptor 1 from a file named "1" (note that 2>1 means to write stderr to the file named "1"), and '&' was one of the few available characters. It's not the address of anything.

To be consistent, it would be &2>&1, but that makes it more verbose than necessary and actually means something else -- the first & means that the command before it runs asynchronously.

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

#36
It means redirect file descriptor 2 to the same destination as file descriptor 1.

Which actually means that an undelrying dup2 operation happens in this direction:

   2 
The file description at [1] is duplicated into [2], thereby [2] points to the same object. Anything written to stderr goes to the same device that stdout is sending to.

The notation follows I/O redirections: cmd > file actually means that a descriptor [n] is first created for the open file, and then that descriptor's decription is duplicated into [1]:

   n 

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

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

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

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

#39
post #22

If you need to know what 2>&1 means, then I would recommend shellcheck It's very, very easy to get shell scripts wrong; for instance the location of the file redirect operator in a pipeline is easy to get wrong.

As someone who use LLMs to generate, among others, Bash script I recommend shellcheck too. Shellcheck catches lots of things and shall really make your Bash scripts better. And if for whatever reason there's an idiom you use all the time that shellcheck doesn't like, you can simply configure shellcheck to ignore that one.

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

#40

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

Useless use of cat error/award

But also | isnt a redirection, it takes stdout and pipes it to another program.

So, if you want stderr to go to stdout, so you can pipe it, you need to do it in order.

bob 2>&1 | prog

You usually dont want to do this though.

Post reply on HN