frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

What does " 2>&1 " mean?

https://stackoverflow.com/questions/818255/what-does-21-mean
76•alexmolas•4h ago

Comments

vessenes•1h ago
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.

ElijahLynn•1h ago
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.
weavie•1h ago
I get the ocassional file named `1` lying around.
LtWorf•45m ago
It's an operator called ">&", the 1 is the parameter.
WJW•31m ago
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.

WhyNotHugo•1h ago
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).
TacticalCoder•33m ago
"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.
anitil•54m ago
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
gnabgib•1h ago
Better: Understanding Linux's File Descriptors: A Deep Dive Into '2>&1' and Redirection https://news.ycombinator.com/item?id=41384919 https://news.ycombinator.com/item?id=39095755
wahern•1h ago
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 redirection operators are executed left-to-right, and traditionally each operator was executed immediately as it was parsed, left-to-right. The pipe operator works similarly, though it's a combination of fork and dup'ing, with the command being forked off from the shell as a child before processing the remainder of the line.

Though, understanding it this way makes the direction of the angled bracket a little odd; at least for me it's more natural to understand dup2(2, 1) as 2<1, as in make fd 2 a duplicate of fd 1, but in terms of abstract I/O semantics that would be misleading.

emmelaich•1h ago
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.

tkcranny•1h ago
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.
ifh-hn•26m ago
Haha, I'm even more confused now. I have no idea what dup is...
jpollock•23m ago
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)
Bender•18m ago
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...

jez•15m ago
Another fun consequence of this is that you can initialize otherwise-unset file descriptors this way:

    $ cat foo.sh
    #!/usr/bin/env bash

    >&1 echo "will print on stdout"
    >&2 echo "will print on stderr"
    >&3 echo "will print on fd 3"

    $ ./foo.sh 3>&1 1>/dev/null 2>/dev/null
    will print on fd 3
It's a trick you can use if you've got a super chatty script or set of scripts, you want to silence or slurp up all of their output, but you still want to allow some mechanism for printing directly to the terminal.

The danger is that if you don't open it before running the script, you'll get an error:

    $ ./foo.sh
    will print on stdout
    will print on stderr
    ./foo.sh: line 5: 3: Bad file descriptor
47282847•3m ago
Interesting. Is this just literally “fun”, or do you see real world use cases?
kccqzy•14m ago
And just like dup2 allows you to duplicate into a brand new file descriptor, shells also allow you to specify bigger numbers so you aren’t restricted to 1 and 2. This can be useful for things like communication between different parts of the same shell script.
zem•1h ago
back when stackoverflow was still good and useful, I asked about some stderr manipulation[0] and learnt a lot from the replies

[0] https://stackoverflow.com/questions/3618078/pipe-only-stderr...

adzm•1h ago
I always wondered if there ever was a standard stream for stdlog which seems useful, and comes up in various places but usually just as an alias to stderr
knfkgklglwjg•53m ago
Powershell has ”stdprogress”
jibal•36m ago
/dev/stderr on Linux
ucarion•1h ago
I've almost never needed any of these, but there's all sorts of weird redirections you can do in GNU Bash: https://www.gnu.org/software/bash/manual/bash.html#Redirecti...
amelius•1h ago
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.

zahlman•1h ago
At the time, the users were the programmers.
booi•1h ago
arguably if you're using the CLI they still are
kube-system•20m ago
nah, we have long had other disciplines using the CLI who do not write their own software, e.g. sysadmins
amelius•1h ago
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.
Msurrow•1h ago
I think he meant that at that time all users were programmers. Yes, _all_ .
andoando•48m ago
programmers are people too! bash syntax just sucks
ifh-hn•23m ago
> and there were >1 users even at the time.

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

HackerThemAll•1h ago
> bash's syntax is so weird

What should be the syntax according to contemporary IT people? JSON? YAML? Or just LLM prompt?

amelius•1h ago
Honestly, Python with the "sh" module is a lot more sane.
Normal_gaussian•13m ago
Is it more sane, or is it just what you are used to?

Python doesn't really have much that makes it a sensible choice for scripting.

Its got some basic data structures and a std-lib, but it comes at a non-trivial performance cost, a massive barrier to getting out of the single thread, and non-trivial overhead when managing downstream processes. It doesn't protect you from any runtime errors (no types, no compile checks). And I wouldn't call python in practice particularly portable...

Laughably, NodeJS is genuinely a better choice - while you don't get multithreading easily, at least you aren't trivially blocked on IO. NodeJS also has pretty great compatibility for portability; and can be easily compiled/transformed to get your types and compile checks if you want. I'd still rather avoid managing downstream processes with it - but at least you know your JSON parsing and manipulation is trivial.

Go is my goto when I'm reaching for more; but (ba)sh is king. You're scripting on the shell because you're mainly gluing other processes together, and this is what (ba)sh is designed to do. There is a learning curve, and there are footguns.

ifh-hn•21m ago
Nushell! Or powershell, but I much prefer nushell!
xeonmc•15m ago
Haskell
csours•1h ago
The conveniences also mean that there is more than ~one~ ~two~ several ways to do something.

Which means that reading someone else's shell script (or awk, or perl, or regex) is INCREDIBLY inconvenient.

amelius•57m ago
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.

ndsipa_pomu•5m ago
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
emmelaich•1h ago
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
inigyou•1h ago
Why would that second one be expected to work?
time4tea•31m ago
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.

kazinator•24m ago
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.

wodenokoto•1h ago
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.
nurettin•1h ago
I saw this newer bash syntax for redirecting all output some years ago on irc

    foo &> file  
    foo |& program
rezonant•1h ago
I didn't know about |&, not sure if it was introduced at the same time. So I'd always use &> for redirection to file and 2>&1 for piping
arjie•1h ago
Redirects are fun but there are way more than I actually routinely use. One thing I do is the file redirects.

    diff <(seq 1 20) <(seq 1 10)
I do that with diff <(xxd -r file.bin) <(xxd -r otherfile.bin) sometimes when I should expect things to line up and want to see where things break.
csours•1h ago
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.

TacticalCoder•32m ago
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.
maxeda•54m ago
> 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.

jibal•43m ago
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.

kazinator•28m ago
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.
kazinator•40m ago
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 <- 1   // dup2(2, 1)
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 <- open("file", O_RDONLY)
   1 <- n
nodesocket•14m ago
I understand how this works, but wouldn’t a more clear syntax be:

command &2>&1

Since the use of & signifies a file descriptor. I get what this ACTUALLY does is run command in the background and then run 2 sending its stout to stdout. That’s completely not obvious by the way.

dheera•11m ago
even clearer syntax:

command &stderr>&stdout

nikeee•7m ago
So if i happen to know the numbers of other file descriptors of the process (listed in /proc), i can redirect to other files opened in the current process? 2>&1234? Or is it restricted to 0/1/2 by the shell?

Would probably be hard to guess since the process may not have opened any file once it started.

Normal_gaussian•3m ago
I know the underlying call, but I always see the redirect symbols as indicating that "everything" on the big side of the operator fits into a small bit of what is on the small side of the operator. Like a funnel for data. I don't know the origin, but I'm believing my fiction is right regardless. It makes <(...) make intuitive sense.

The comment about "why not &2>&1" is probably the best one on the page, with the answer essentially being that it would complicate the parser too much / add an unnecessary byte to scripts.

Statement from Dario Amodei on Our Discussions with the Department of War

https://www.anthropic.com/news/statement-department-of-war
459•qwertox•1h ago•241 comments

Smartphone Mkt to Decline 13% in '26, Largest Drop Ever Due to Memory Shortage

https://www.idc.com/resource-center/press-releases/wwsmartphoneforecast4q25/
124•littlexsparkee•2h ago•118 comments

Layoffs at Block

https://twitter.com/jack/status/2027129697092731343
308•mlex•2h ago•286 comments

AirSnitch: Demystifying and breaking client isolation in Wi-Fi networks [pdf]

https://www.ndss-symposium.org/wp-content/uploads/2026-f1282-paper.pdf
311•DamnInteresting•8h ago•155 comments

Will vibe coding end like the maker movement?

https://read.technically.dev/p/vibe-coding-and-the-maker-movement
281•itunpredictable•8h ago•276 comments

What Claude Code Chooses

https://amplifying.ai/research/claude-code-picks
201•tin7in•6h ago•91 comments

Launch HN: Cardboard (YC W26) – Agentic video editor

https://www.usecardboard.com/
88•sxmawl•5h ago•46 comments

Hydroph0bia – fixed SecureBoot bypass for UEFI firmware from Insyde H2O (2025)

https://coderush.me/hydroph0bia-part3/
17•transpute•2h ago•1 comments

Anthropic says company 'cannot in good conscience accede' to Pentagon's demands

https://apnews.com/article/anthropic-ai-pentagon-hegseth-dario-amodei-9b28dda41bdb52b6a378fa9fc80...
44•geox•1h ago•8 comments

Understanding the Go Runtime: The Memory Allocator

https://internals-for-interns.com/posts/go-memory-allocator/
18•valyala•3d ago•5 comments

What does " 2>&1 " mean?

https://stackoverflow.com/questions/818255/what-does-21-mean
78•alexmolas•4h ago•59 comments

OsmAnd's Faster Offline Navigation (2025)

https://osmand.net/blog/fast-routing/
105•todsacerdoti•5h ago•26 comments

I baked a pie every day for a year and it changed my life

https://www.theguardian.com/lifeandstyle/2026/feb/22/a-new-start-after-60-i-baked-a-pie-every-day...
200•NaOH•3d ago•132 comments

Lidar waveforms are worth 40x128x33 words

https://openaccess.thecvf.com/content/ICCV2025/html/Scheuble_Lidar_Waveforms_are_Worth_40x128x33_...
30•teleforce•3d ago•11 comments

Museum of Plugs and Sockets

https://plugsocketmuseum.nl/index.html
62•ohjeez•3d ago•20 comments

Palm OS User Interface Guidelines (2003) [pdf]

https://cs.uml.edu/~fredm/courses/91.308-spr05/files/palmdocs/uiguidelines.pdf
140•spiffytech•7h ago•64 comments

Netflix Backs Out of Warner Bros. Bidding, Paramount Set to Win

https://www.hollywoodreporter.com/business/business-news/netflix-backs-out-warners-deal-paramount...
44•atombender•59m ago•17 comments

Show HN: Deff – Side-by-side Git diff review in your terminal

https://github.com/flamestro/deff
75•flamestro•6h ago•47 comments

Show HN: Hacker Smacker – Spot great (and terrible) HN commenters at a glance

https://hackersmacker.org
83•conesus•2d ago•76 comments

An Introduction to the Codex Seraphinianus, the Strangest Book Ever Published

https://www.openculture.com/2026/02/an-introduction-to-the-codex-seraphinianus.html
4•vinhnx•3d ago•1 comments

Bild AI (YC W25) Is Hiring Interns to Make Housing Affordable

https://www.workatastartup.com/jobs/80596
1•rooppal•7h ago

BuildKit: Docker's Hidden Gem That Can Build Almost Anything

https://tuananh.net/2026/02/25/buildkit-docker-hidden-gem/
137•jasonpeacock•10h ago•41 comments

Show HN: Terminal Phone – E2EE Walkie Talkie from the Command Line

https://gitlab.com/here_forawhile/terminalphone
281•smalltorch•13h ago•71 comments

Show HN: Linex – A daily challenge: placing pieces on a board that fights back

https://www.playlinex.com/
44•Humanista75•2d ago•19 comments

Nano Banana 2: Google's latest AI image generation model

https://blog.google/innovation-and-ai/technology/ai/nano-banana-2/
469•davidbarker•8h ago•454 comments

The Wolfram S Combinator Challenge

https://www.combinatorprize.org/
62•paraschopra•3d ago•20 comments

Steering interpretable language models with concept algebra

https://www.guidelabs.ai/post/steerling-steering-8b/
48•luulinh90s•1d ago•3 comments

This time is different

https://shkspr.mobi/blog/2026/02/this-time-is-different/
104•speckx•10h ago•160 comments

Banned in California

https://www.bannedincalifornia.org/
541•pie_flavor•1d ago•643 comments

Open Source Endowment – new funding source for open source maintainers

https://endowment.dev/
203•kvinogradov•8h ago•125 comments