Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

151–160 of 260 posts

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

#151

Earlier quoted context omitted.

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

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…

What a headache!

My practical view is to avoid spaces in directories and filenames, but to write scripts that handle them just fine (using BASH - I'm guilty of using it when more sane people would be using a proper language).

My ideological view is that unix/POSIX filenames are allowed to use any character except for NULL, so tools should respect that and handle files/dirs correctly.

I suppose for your usage, it'd be better to put the virtualenv directory into your path and then use #!/usr/bin/env python

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

#152
post #49

Earlier quoted context omitted.

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

I love that this situation occured.

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

#153

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 possible for misguided product initiatives. It's just wild yo

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

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

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.

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

#155

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…

Although PowerShell borrows the syntax, it (as usual!) completely screws up the semantics. The examples in the docs [1] show first setting descriptor 2 to descriptor 1 and then setting descriptor 1 to a newly opened file, which of course is backwards and doesn't give the intended result in Unix; e.g. their example 1:

  dir C:\, fakepath 2>&1 > .\dir.log
Also, according to the same docs, the operators "now preserve the byte-stream data when redirecting output from a native command" starting with PowerShell 7.4, i.e. they presumably corrupted data in all previous versions, including version 5.1 that is still bundled with Windows. And it apparently still does so, mysteriously, "when redirecting stderr output to stdout".

[1] https://learn.microsoft.com/en-us/powershell/module/microsof...

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

#157
post #134

Earlier quoted context omitted.

This is probably one of the reasons why many find POSIX shell languages to be unpleasant. There are too many syntactical sugars that abstract too much of the underlying mechanisms away, to the level that we don't get it unless someone explains it. Compare this with Lisps, for example. There may be only one branching construct or a looping construct. Yet, they provide more options than regular programming languages us…

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…

Seems related to the Law of Leaky Abstractions?

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

#158
post #55

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.

even clearer syntax: command &stderr>&stdout

[deleted]

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

#159
post #55

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.

even clearer syntax: command &stderr>&stdout

You're not limited to the standard file descriptors.

  command 4>&3
Post reply on HN