Live data from Hacker News

What does " 2>&1 " mean?

stackoverflow.com

111–120 of 260 posts

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

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

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

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

#112

Earlier quoted context omitted.

> bash's syntax is so weird What should be the syntax according to contemporary IT people? JSON? YAML? Or just LLM prompt?

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?

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

#113
post #3

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

O'Reilly's Essential System Administration [1], I never do a job interview without it.

[1]: https://www.oreilly.com/library/view/essential-system-admini...

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

#114

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.

You can pipe the fd directly:

# echo 1 >&2 2>| echo

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

#115
post #62

Earlier quoted context omitted.

Multiple levels of logging, all of which you want to capture but not all in the same place.

Wasn't the idiomatic way the `-v` flag (repeated for verbosity). And then stderr for errors (maybe warning too).

It is, and all logs should ideally go to stderr. But that doesn’t let you pipe them to different places.

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

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

Respectfully, what was the purpose of this comment, really?

And I also disagree, your suggestion is not easier. The & operator is quite intuitive as it is, and conveys the intention.

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

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

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

#118

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.

You shouldn't be assuming I'm writing code for Unix.

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

#120

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.

lol truly informative and clearly something no one here knew. But your terminology is inaccurate. Please change it to GNU/Linux != Unix
Post reply on HN