Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

231–240 of 260 posts

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

#231
post #224
post #212

Earlier quoted context omitted.

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 doe…

> 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. Where else would you look but in the manual of your shell? And you don’t have to know in which section to look, you can just search for “2>&1” in the bash man page.

What is a command and what is shell syntax is not always obvious, especially to a beginner, which I assume most people asking this question are.

Take the command "ls -l ~/.. ; fg" for instance. What is interpreted by the shell and what are commands? If you have some experience in bash, you probably know, and therefore you know which part to look in which man page, but you probably also know "2>&1".

Spoiler: "-l" is part of the command, so look in the "ls" manpage. "~", is expanded by the shell, ";" is shell syntax and "fg" is a builtin, all three are in the "bash" manpage. ".." is part of the filesystem.

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

#232
I understood the point of the question was how shells work seems very context driven. An & here means something different to an & there. IFS=\| read A B C <<< "first|second|third" the read is executed and the IFS assignment is local to the one command echo hello this will "hello this", even though in the assignment above the space was important an & at the end of a line is run the task background and in the middle of the redirect isn't. All these things can be learned, but it's hard to explain the patterns, I think.

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

#234

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.

Try it without the `cat` and tell me what you get.

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

#236

Man I miss stack overflow. It feels so much better to ask humans a question then the machine, but it feels impossible to put the lid back on the box.

It's really jarring to see this wave of nostalgia for "the good old days" appear since ~2025. Suddenly these rose tinted glasses have dropped and everything before LLM usage became ubiquitous was a beautiful romantic era of human collaboration, understanding and craftsmanship. I still acutely remember the gatekeeping and hostility of peak stack overflow, and the inanity of churning out jira tickets as fast as possibl…

I think most people found StackOverflow to be pretty easy and useful since it's a pretty small minority of people that ever asked questions on it so many people didn't interact at all with the more annoying parts.

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

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

I did a google search on “dup2(2, 1)” in a fresh private tab on my iPhone Safari and this thread came up the second, between

https://man7.org/linux/man-pages/man2/dup.2.html

and

https://man.archlinux.org/man/dup2.2.en

A lot of bots are reading this. Amazing.

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

#238
post #29

Earlier quoted context omitted.

Yes. There are many reasons why one shouldn't use sh/bash for scripting. But my main reason is that most scripts break when you call them with filenames that contain spaces. And they break spectacularly.

You're not wrong, but there's fairly easy ways to deal with filenames containing spaces - usually just enclosing any variable use within double quotes will be sufficient. It's tricker to deal with filenames that contain things such as line breaks as that usually involves using null terminated filenames (null being the only character that is not allowed in filenames). e.g find . -type f -print0

Ah, but then there are the unusual cases. See “The shell and its crappy handling of whitespace.”

https://blog.plover.com/Unix/whitespace.html

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

#239

Earlier quoted context omitted.

> 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

I do and it does.

    $ ls -al /dev/std*
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stderr -> fd/2
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdin -> fd/0
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdout -> fd/1
    $ ls -n /dev/fd/[012]
    crw--w----  1 501  4  0x10000000 Feb 27 13:38 /dev/fd/0
    crw--w----  1 501  4  0x10000000 Feb 27 13:38 /dev/fd/1
    crw--w----  1 501  4  0x10000000 Feb 27 13:38 /dev/fd/2
    $ uname -v
    Darwin Kernel Version 24.6.0: Mon Jan 19 22:00:55 PST 2026; root:xnu-11417.140.69.708.3~1/RELEASE_ARM64_T6000
    $ sw_vers
    ProductName:  macOS
    ProductVersion:  15.7.4
    BuildVersion:  24G517
Lest you think it's some bashism that's wrapping ls, they exist regardless of shell:

    $ zsh -c 'ls -al /dev/std*'
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stderr -> fd/2
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdin -> fd/0
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdout -> fd/1
    $ csh -c 'ls -al /dev/std*'
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stderr -> fd/2
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdin -> fd/0
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdout -> fd/1
    $ tcsh -c 'ls -al /dev/std*'
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stderr -> fd/2
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdin -> fd/0
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdout -> fd/1
    $ ksh -c 'ls -al /dev/std*'
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stderr -> fd/2
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdin -> fd/0
    lr-xr-xr-x  1 root  wheel  0 Feb 24 15:08 /dev/stdout -> fd/1
I tried the install example that you provided and it worked on macOS as well as Linux.

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

#240

Earlier quoted context omitted.

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.

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

It's not that hard. Consider the following:

  $ command &2>&1
The shell thinks that you're trying to run the portion before the & (command) in the background and the portion after the & (2>&1) in the foreground. There is just one problem. The second part (2>&1) means that you're redirecting stderr/fd2 to stdout/fd1 for a command that is to follow (similar to how you set environment variables for a command invocation). However, you haven't specified the command that follows. The second part just freezes waiting for the command. Try it and see for yourself.

  $ command 2>1
Here the shell redirects the output of stderr/fd2 to a file named 1. It doesn't know that you're talking about a file descriptor instead of a filename. So you need to use &1 to indicate your intention. The same confusion doesn't happen for the left side (fd2) because that will always be a file descriptor. Hence the correct form is:

  $ command 2>&1
> I think shell language is simply awful.

Honestly, I wish I could ask the person who designed it, why they made such decisions.

Post reply on HN