Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

251–260 of 260 posts

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

#251
post #240

Earlier quoted context omitted.

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

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

https://web.archive.org/web/20250115051355/https://www.bell-...

https://www.cs.dartmouth.edu/~doug/sieve/sieve.pdf

https://www.in-ulm.de/~mascheck/bourne/index.html#origins

They also include citations to papers by Thompson, Bourne, and others.

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

#252

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.

Handles and pointers are the same concept, the difference is just who resolves them. Pointers don't represent hardware addresses either.

Well, if we reduce it enough I supposed they can be seen as the same concept through a certain kind of philosophical lense. True and false also belong in the same class, they're handles to a pool of two possible boolean values.

The difference is in scope. Pointers (aka memory addresses) are an ordered set of numbers enumerating all the memory locations, it enables unique powerful properties that enable a large set of uses that you cannot do with handles. And also make them quite unsafe and harder to understand.

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

#253
post #244

Earlier quoted context omitted.

> The "Redirections" section of the manual [0] is just seven US Letter pages. "Just" seven US Letter pages? You're talking about redirections alone, right? How many such features exist in Bash? I find Python, Perl and even Lisps easier to understand. Some of those languages wouldn't have been even conceived if shell languages were good enough. There is another shell language called 'execline' (to be precise, it's a r…

> "Just" seven US Letter pages? Yes. It's the syntax alongside prose explaining the behavior in detail. Go give it a read. If you want documentation that's done up in the "modern" style, then you'll prefer that one-page cheat sheet that that guy made. I find that "modern" documentation tends to leave it up to each reader to discover the non-obvious parts of the behavior for themselves. > I find Python ... easier to u…

> Yes. It's the syntax alongside prose explaining the behavior in detail. Go give it a read.

Bold of you to assume that I or the others didn't. I made my statement in spite of reading it. Not because I didn't read it. So my opinion is unchanged here.

The point here is simple. Documentation is a very important addition. But you can't paper over other deficiencies with documentation, especially if you find yourself referring the same documentation again and again. It's an indication that you're dealing with an abstraction that can't easily be internalized. Throwing the book at everyone isn't a good solution to every problem.

> Have you read the [0] docs for Python's 'subprocess' library? The ...

Yes, I have! All of those. Their difference with bash documentation is that you get the idea in a single glance. I spend much less time wondering how to make sense of it all. Python's abstractions are well thought out, carefully selected, consistently and orthogonally implemented and stays out of the way - something I can hardly say about bash. If that's not enough for you, Python has something that bash lacks - PEPs. The documents that neatly outline the rationale behind their decisions. That's what a lot of programmers want to know and every programmer should know.

Fun fact: The Epstein files contain a copy of the bash manual! Of course they weren't involved in his crimes. It was just one of the documents found on his system. A sysadmin is believed to have downloaded it for reference. But it's telling that it wasn't the Python manual, or the Perl manual, or something else. Meanwhile, I don't really think that Epstein was running Linux on his system.

> Unless you want to underdocument and leave important parts of the behavior for users to incorrectly guess, such documentation is going to be much larger than a cheat sheet would be.

If properly designed, such expansive documentation would be unnecessary, as they would be obvious even with the abstractions. For example when you use a buffer abstraction in modern languages, you have a fairly good idea what it does and why you need it, even though you may not care about its exact implementation details. That's the sort of quality where bash and other POSIX shells fail on several counts. In fact, check how many other shells break POSIX compatibility to solve this problem. Fish and nushell, for example.

"The developer is too lazy to read the documentation" isn't the appropriate stance to assume when so many are expressing their frustration and displeasure at it. At some point, you have to concede that there are genuine problems that cannot be blamed on the developer alone.

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

#254
post #253

Earlier quoted context omitted.

> "Just" seven US Letter pages? Yes. It's the syntax alongside prose explaining the behavior in detail. Go give it a read. If you want documentation that's done up in the "modern" style, then you'll prefer that one-page cheat sheet that that guy made. I find that "modern" documentation tends to leave it up to each reader to discover the non-obvious parts of the behavior for themselves. > I find Python ... easier to u…

> Yes. It's the syntax alongside prose explaining the behavior in detail. Go give it a read. Bold of you to assume that I or the others didn't. I made my statement in spite of reading it. Not because I didn't read it. So my opinion is unchanged here. The point here is simple. Documentation is a very important addition. But you can't paper over other deficiencies with documentation, especially if you find yourself ref…

> But you can't paper over other deficiencies with documentation, especially if you find yourself referring the same documentation again and again. It's an indication that you're dealing with an abstraction that can't easily be internalized.

> Their difference with bash documentation is that you get the idea in a single glance.

> If properly designed, such expansive documentation would be unnecessary, as they would be obvious even with the abstractions.

What is it the kids say? "Tell me you don't make use of 'multiprocessing', 'subprocess', and other such inherently-complicated modules without telling that you don't..."? Well, it's either that, or you that often use them, and rarely use bash I/O redirections... because, man, the docs for just the 'subprocess.Popen' constructor are massive and full of caveats and warnings.

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

#255
post #243

Earlier quoted context omitted.

? (defun even(num) (= (mod num 2) 0)) ? (filter '(6 4 3 5 2) #'even) I'm zero Lisp expert and I don't feel comfortable at all reading this snippet.

This: > I'm zero Lisp expert and this: > I don't feel comfortable at all reading this snippet are related. The comfort in reading Lisp comes from how few syntactic/semantic rules there are. There's a standard form and a few special forms. Compare that to C - possibly one of the smallest popular languages around. How many syntactical and semantic rules do you need to know to be a half decent C programmer? If you look…

The initial discussion was about bash syntax. I do understand that exceptions to rules are what make a language more complicated (either human or computer language, it doesn't matter), but also a language barrier of entry is a very important factor in how complicated a language is.

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

#256

Earlier quoted context omitted.

I've long wanted easy, trivial multichannel i/o with duplication I want to be able to route x independent input and y independent output trivially from the terminal Proper i/o routing It shouldn't be hard, it shouldn't be unsolved, and it shouldn't be esoteric

That's what named pipes do.

Some kind of elegant unixy syntax would be nice

[ ... Inputs ] | command | [ ... Outputs ]

Basically select(1) as a cli syntax.

I've done quite a bit of unix historical work ... Not enough for a talk at the CHM but decent enough that I have interviewed dozens of people.

I really think some basic stuff was just left in a hacky state and we never revisited the primitives right.

I've been trying to do that in my own projects

For instance I should be able to do something like

Command || processor

And not have processor hijack the input without hacky pty stuff. I am intentionally using || here.

There's lots of use cases to this: llms are the best, logging, rendering text, readline, translation, accessibility, it'd be a very useful primitive and it's impossible to do without a full pty wrapper or some kind of voodoo heuristic wrangling.

Currently you have to do things like this https://github.com/day50-dev/ESChatch/blob/main/eschatch.py#...

It should be easy

I know that some esoteric shells do it but I want everything to be traditional with better i/o features

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

#257
post #253

Earlier quoted context omitted.

> Yes. It's the syntax alongside prose explaining the behavior in detail. Go give it a read. Bold of you to assume that I or the others didn't. I made my statement in spite of reading it. Not because I didn't read it. So my opinion is unchanged here. The point here is simple. Documentation is a very important addition. But you can't paper over other deficiencies with documentation, especially if you find yourself ref…

> But you can't paper over other deficiencies with documentation, especially if you find yourself referring the same documentation again and again. It's an indication that you're dealing with an abstraction that can't easily be internalized. > Their difference with bash documentation is that you get the idea in a single glance. > If properly designed, such expansive documentation would be unnecessary, as they would b…

You're resorting to non sequiturs, nitpicking and vague assertions to just skirt around the point here. Python syntax rarely confuses people as much as bash does. Look at this entire discussion list for example.

subprocess module isn't a reasonable example to the contrary, because it isn't Python's syntactical sugar that makes it confusing. And even in case of modules that aren't well designed, the language developers and the community strive to provide a more ergonomic alternative.

But instead of addressing the point, you decided to make it about me and my development patterns based on some wild reasoning. But that's not surprising because this started with you asserting that it's the developers' fault that bash appears so confusing to them. Just some worthless condescension instead of staying on topic. What a disgrace!

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

#258

Earlier quoted context omitted.

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

Don't know if this is definitive, but:

https://www.johndcook.com/powershell.html#:~:text=The%20core...

POSIX Korn shell, specifically, according to Wikipedia:

https://en.wikipedia.org/wiki/PowerShell#Grammar

so maybe it inherited 2>&1 from Korn shell, which in turn was POSIX.

But yeah, Powershell was not built purely to be a POSIX shell, but I thought it tried to be compatible where it made sense (hence the seeming clash of cultures).

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

#259

Earlier quoted context omitted.

You're not wrong, but at my place, our main repository does not permit cloning into a directory with spaces in it. Three factors conspire to make a bug: 1. Someone decides to use a space 2. We use Python 3. macOS Say you clone into a directory with a space in it. We use Python, so thus our scripts are scripts in the Unix sense. (So, Python here is replacable with any scripting language that uses a shebang, so long as…

These are part of the rituals of learning how a system works, in the same way interns get tripped up at first when they discover ^S will hang an xterm, until ^Q frees it. If you're aware of the history of it, it makes perfect sense. Unix has a personality, and in this case the kernel needs to decide what executable to run before any shell is involved, so it deliberately avoids the complexity of quoting rules. I'd giv…

> I'd give this a try, works with any language:

  #!/usr/bin/env -S "/path/with spaces/my interpreter" --flag1 --flag2
This won't do what you're thinking it does. If I run that, I get:

  env: No terminating quote for string: /path/with"/path/with
… because the string you've given env -S on my system is malformed, and lacks a terminating quote. (You can test this w/ just giving an unterminated quoted string to env … I have no idea why the messaging is so funky looking, but that's sort of par for the course here.)

As I alluded to in my post, shebangs don't handle escaping. Now, yes, you're thinking that env will do it, here. The other problem with shebangs is that they're ridiculously unstandardized. On Linux, for example, that shebang will parse out to:

  #!/usr/bin/env -S "/path/with spaces/my interpreter" --flag1 --flag2

  argv[0]: "/usr/bin/env"
  argv[1]: "-S"
  argv[2]: "\"/path/with spaces/my interpreter\" --flag1 --flag2"
  argv[3]: 
& then -S proceeds as you expect it to. Things appear to work.

On my system,

  #!/usr/bin/env -S "/path/with spaces/my interpreter" --flag1 --flag2

  argv[0]: "/usr/bin/env"
  argv[1]: "-S"
  argv[2]: "\"/path/with"
  argv[3]: "spaces/my"
  argv[4]: "interpreter"
  argv[5]: "--flag1"
  argv[6]: "--flag2"
  argv[7]: 
This is because Linux passes everything after the first space as a single arg. macOS splits on spaces, but does no further processing (such as some form of backslash escapes) beyond that.

Since,

  env -S '"/path/with' 
is nonsense, env errors out with the above error.

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

#260

Earlier quoted context omitted.

These are part of the rituals of learning how a system works, in the same way interns get tripped up at first when they discover ^S will hang an xterm, until ^Q frees it. If you're aware of the history of it, it makes perfect sense. Unix has a personality, and in this case the kernel needs to decide what executable to run before any shell is involved, so it deliberately avoids the complexity of quoting rules. I'd giv…

> I'd give this a try, works with any language: #!/usr/bin/env -S "/path/with spaces/my interpreter" --flag1 --flag2 This won't do what you're thinking it does. If I run that, I get: env: No terminating quote for string: /path/with"/path/with … because the string you've given env -S on my system is malformed, and lacks a terminating quote. (You can test this w/ just giving an unterminated quoted string to env … I hav…

It works fine with GNU env with -S support, and a GNU-compatible kernel. I'm aware that won't work on some other systems, hence the 9 other examples. I said I would try that first and see how it goes, and low and behold it works fine on the systems I use.

  $ cat bbb.ml 
  #!/usr/bin/env -S "/home/user/.local/bin/o c a m l" -no-version
  print_endline "ok";;
  $ ls -lh ~/.local/bin/"o c a m l"
  lrwxrwxrwx 1 user user 14 Feb 27 07:26 '/home/user/.local/bin/o c a m l' -> /usr/bin/ocaml
  $ chmod a+rx bbb.ml
  $ ./bbb.ml
  ok
  $ 
But if it didn't work, you can get pretty good mileage out of abusing sh to get the job done for many popular languages.
Post reply on HN