Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

121–130 of 260 posts

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

#121
post #98

Earlier quoted context omitted.

if statements are pretty simple if $command; then else fi You may be complaining about the syntax for the test command specifically or bash’s [[ builtin Also the choice of quotes changing behavior is a thing in: 1. JavaScript/typescript 2. Python 3. C/C++ 4. Rust In some cases it’s the same difference, eg: string interpolation in JavaScript with backticks

> Also the choice of quotes changing behavior is a thing in: In those languages they change what's contained in the string. Not how many strings you get. Or what the strings from that string look like. ($@ being an extreme example)

> $@ being an extreme example

From the bash man page via StackOverflow:

> @ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

That’s…a lot. I think Bash is interesting in the “I’m glad it works but I detest having to work with it” kind of way. Like, fine if I’m just launching some processes or tail’ing some logs, but I’ve rarely had a time when I had to write an even vaguely complex bash script where I didn’t end up spending most of my time relearning how to do things that should be basic.

Shellcheck was a big game changer at least in terms of learning some of the nuance from a “best practice” standpoint. I also think that the way bash does things is just a little too foreign from the rest of my computing life to be retained.

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

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

They're more like capabilities or handles than pointers. There's a reason in Rust land many systems use handles (indices to a table of objects) in absence of pointer arithmetic. In the C API of course there's symbolic names for these. STDIN_FILENO, STDOUT_FILENO, etc for the defaults and variables for the dynamically assigned ones.

What they point to are capabilities, but the integer handles that user space gets are annoyingly like pointers. In some respects, better, since we don’t do arithmetic on them, but in others, worse: they’re not randomized, and I’ve never come across a sanitizer (in the ASan sense) for them, so they’re vulnerable to worse race condition and use-after-free issues where data can be quietly sent to the entirely wrong place. Unlike raw pointers’ issues, this can’t even be solved at a language level. And maybe worst of all, there’s no bug locality: you can accidentally close the descriptor backing a `FILE*` just by passing the wrong small integer to `close` in an unrelated part of the program, and then it’ll get swapped out at the earliest opportunity.

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

#123

The comments on stackoverflow say the words out of my mouth so I'll just copy & paste here: > but then shouldn't it rather be &2>&1? > & is only interpreted to mean "file descriptor" in the context of redirections. Writing command &2>& is parsed as command & and 2>&1 That's where all the confusion comes from. I believe most people can intuitively understand > is redirection, but the asymmetrical use of & throws them…

Isn't that because of posix?

Powershell is not posix compliant and does not pretend to be. Like conditionals using `()` instead of `[]` is already a clear departure from posix

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

#124

Earlier quoted context omitted.

Nushell, Powershell, Python, Ruby, heck even Perl is better. Shell scripting is literally the worst language I've ever seen in common use. Any realistic alternative is going to be better.

It always exists on any Unix system. Even a busybox root environment. Why do you want to save a few bytes to compromise portability?

But it isn't portable, unless you stick to posix subset which kinda sucks. You'll use some feature that some dude using an ancient shell doesn't have then he'll complain to you. And that list of features is LONG: https://oneuptime.com/blog/post/2026-02-13-posix-shell-compa...

If you're using shell specific features in a tightly controlled environment like a docker container then yeah, go wild. If you're writing a script for personal use, sure. If you're writing something for other people to run then your code will be working around all the missing features posix hasn't been updated to include. You can't use arrays, or arithmetic context, nothing. It sucks to use.

Besides, if you're writing a script it is likely that it will grow, get more complicated, and you will soon bump up against the limitations of the language and have to do truly horrible workarounds.

This is why if I need something for others to run then I just use python from the beginning. The code will be easier to read and more portable. At this point the vast majority of OS's and images have it available anyway so it's not as big a barrier as it used to be.

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

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

[flagged]

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

#126

Earlier quoted context omitted.

It should be a lesson to learn on how simple, logical and reliable tools can last decades.

Bash syntax is anything but simple or logical. Just look at the insane if-statement syntax. Or how the choice of quotes fundamentally changes behavior. Argument parsing, looping, the list goes on.

Are taxes simple?

Why does Bash syntax have to be "simple"? For me, Bash syntax is simple.

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

#127

Earlier quoted context omitted.

Interesting. Is this just literally “fun”, or do you see real world use cases?

The aws cli has a set of porcelain for s3 access (aws s3) and plumbing commands for lower level access to advanced controls (aws s3api). The plumbing command aws s3api get-object doesn't support stdout natively, so if you need it and want to use it in a pipeline (e.g. pv), you would naively do something like $ aws s3api get-object --bucket foo --key bar /dev/stdout | pv ... Unfortunately, aws s3api already prints the…

Would be nice if `curl` had something to dump headers to a third file descriptor while outputting the response on stdout.

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

#128

Earlier quoted context omitted.

They're more like capabilities or handles than pointers. There's a reason in Rust land many systems use handles (indices to a table of objects) in absence of pointer arithmetic. In the C API of course there's symbolic names for these. STDIN_FILENO, STDOUT_FILENO, etc for the defaults and variables for the dynamically assigned ones.

What they point to are capabilities, but the integer handles that user space gets are annoyingly like pointers. In some respects, better, since we don’t do arithmetic on them, but in others, worse: they’re not randomized, and I’ve never come across a sanitizer (in the ASan sense) for them, so they’re vulnerable to worse race condition and use-after-free issues where data can be quietly sent to the entirely wrong plac…

BITD the one "fd sanitizer" I ever encountered was "try using the code on VxWorks" which at the time was "posix inspired" at best - fds actually were pointers, so effectively random and not small integers. It didn't catch enough things to be worth the trouble, but it did clean up some network code (ISTR I was working on SNTP and Kerberos v4 and Kerberized FTP when I ran into this...)

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

#129

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 is possible. Many people choose a healthy lifestyle instead of becoming morbidly obese and incapable which is easy to do in our society.

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

#130

Earlier quoted context omitted.

> At least allow us to use names instead of numbers. You can use /dev/stdin, /dev/stdout, /dev/stderr in most cases, but it's not perfect.

> You can use /dev/stdin, /dev/stdout, /dev/stderr in most cases Never ever write code that assumes this. These dev shorthands are Linux specific, and you'll even need a certain minimum Linux version. I cringe at the amount of shell scripts that assume bash is the system interpreter, and not sh or ksh. Always assume sh, it's the most portable. Linux != Unix.

Actually, while the Actual Nodes are a linux thing, bash itself implements (and documents) them directly (in redirections only), along with /dev/tcp and /dev/udp (you can show with strace that bash doesn't reference the filesystem for these, even if they're present.)

So, you're not wrong, but...

Post reply on HN