Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

41–50 of 260 posts

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

#41
post #34

Earlier quoted context omitted.

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.

Well sure, but surely this takes some inspiration from both `&` as the "address of" operator in C as well as the `>` operator which (apart from being the greater-than operator) very much implies "into" in many circumstances.

So `>&1` is "into the file descriptor pointed to by 1", and at the time any reasonable programmer would have known that fd 1 == STDOUT.

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

#42
post #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…

It's not inconsistent. The & is attached to the redirection operator, not to the 1 token. The file descriptor being redirected is also attached:

Thus you cannot write:

  2 > &1

You also cannot write

  2 >& 1
However you may write

  2>& 1
The n>& is one clump.

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

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

Haha, I'm even more confused now. I have no idea what dup is...

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

#44

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.

The point is that the order in which that is processed is not left to right.

First the | pipe is established as fd [1]. And then 2>&1 duplicates that pipe into [2]. I.e. right to left: opposite to left-to-right processing of redirections.

When you need to capture both standard error and standard output to a file, you must have them in this order:

  bob > file 2>&1
It cannot be:

  bob 2>&1 > file
Because then the 2>&1 redirection is performed first (and usually does nothing because stderr and stdout are already the same, pointing to your terminal). Then > file redirects only stdout.

But if you change > file to | process, then it's fine! process gets the combined error and regular output.

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

#45
post #43
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…

Haha, I'm even more confused now. I have no idea what dup is...

There are a couple of ways to figure out.

open a terminal (OSX/Linux) and type:

    man dup
open a browser window and search for:

    man dup
Both will bring up the man page for the function call.

To get recursive, you can try:

    man man unix
(the unix is important, otherwise it gives you manly men)

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

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

> and there were >1 users even at the time.

Are you sure there wasn't >&1 users... Sorry I'll get my coat.

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

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

Nushell! Or powershell, but I much prefer nushell!

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

#49
post #43

Earlier quoted context omitted.

Haha, I'm even more confused now. I have no idea what dup is...

There are a couple of ways to figure out. open a terminal (OSX/Linux) and type: man dup open a browser window and search for: man dup Both will bring up the man page for the function call. To get recursive, you can try: man man unix (the unix is important, otherwise it gives you manly men)

otherwise it gives you manly men

That's only just after midnight [1][2]

[1] - https://www.youtube.com/watch?v=XEjLoHdbVeE

[2] - https://unix.stackexchange.com/questions/405783/why-does-man...

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

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

Haskell
Post reply on HN