this post was submitted on 23 Jul 2025
43 points (100.0% liked)

Linux

56731 readers
699 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 6 years ago
MODERATORS
 

In bash, if you put:

ls /Users/*/.ssh/id_rsa 2>&1 > rsa-keys.log

...you're redirecting stderr to the stdout's destination while stdout is still sending output to the screen. So any permission errors encountered will go to the screen, not to rsa-keys.log.

From the bash manpage:

==================

Note that the order of redirections is significant. For example, the command

   ls > dirlist 2>&1

directs both standard output and standard error to the file dirlist, while the command

   ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was duplicated from the standard output before the standard output was redirected to dirlist.

==================

Commands given to the shell are evaluated and processed in a specific order and fashion, and this is one quirk of that that many people are unaware of.

top 19 comments
sorted by: hot top controversial new old
[–] racketlauncher831@lemmy.ml 9 points 3 days ago (1 children)

As true as this can be, it's counterintuitive.

[–] AndrasKrigare@beehaw.org 2 points 3 days ago

What would be more intuitive? It seems to me to be a

a=1 b=a a=2

Where you'd expect b to be 1, which is the case for bash.

[–] the_crotch@sh.itjust.works 2 points 2 days ago (2 children)

What distro uses /Users instead of /home? Doesn't that break the standard?

[–] samc@feddit.uk 8 points 2 days ago (1 children)
[–] the_crotch@sh.itjust.works 1 points 2 days ago

I used macs for 15 years I really should have known that lol.

[–] lousyd@lemmy.sdf.org 1 points 2 days ago
[–] exu@feditown.com 7 points 3 days ago (1 children)

I never use more than one redirection to keep it simple. Stuff gets even weirder when you also use < in the same lines

[–] MonkderVierte@lemmy.zip 1 points 3 days ago

envsubst is a exception.

[–] korbel@lemmy.ml 2 points 3 days ago

In bash if you want to redirect both stderr and stdout to file you can use &>filename.

[–] INeedMana@piefed.zip 3 points 3 days ago (2 children)

IMO |& tee dirlist is easier to manage

[–] lousyd@lemmy.sdf.org 4 points 3 days ago (1 children)

Your way would spawn a whole extra process, but if you're running Bash commands I suppose that doesn't often matter.

[–] INeedMana@piefed.zip 1 points 3 days ago (1 children)

For 2>&1 to work don't we need to be using some shell anyway?

[–] lousyd@lemmy.sdf.org 2 points 3 days ago

Yeah. The shell, plus whatever command (or commands) you tell the shell to run.

[–] MonkderVierte@lemmy.zip 1 points 3 days ago (1 children)
[–] INeedMana@piefed.zip 1 points 3 days ago (1 children)

https://www.man7.org/linux/man-pages/man1/bash.1.html

Pipelines A pipeline is a sequence of one or more commands separated by one of the control operators | or |&. The format for a pipeline is:

[time [-p]] [ ! ] command1 [ [|⎪|&] command2 ... ]

(...) If |& is used, command1's standard error, in addition to its standard output, is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error to the standard output is performed after any redirections specified by command1.

[–] MonkderVierte@lemmy.zip 1 points 3 days ago* (last edited 3 days ago) (1 children)

So it is a bash thing.

Just to be clear, because some seem to conflate Bash with Shell. If not specified, assume POSIX shell, that's how /bin/sh is handled as well. And that has no |&.

[–] INeedMana@piefed.zip 1 points 3 days ago (1 children)

True

But nowadays /bin/sh is often just a link to bash

[–] MonkderVierte@lemmy.zip 1 points 3 days ago* (last edited 3 days ago) (1 children)

Which puts Bash in POSIX compliance mode.

[–] INeedMana@piefed.zip 2 points 3 days ago
$ sh
sh-5.2$ echo dfgsdfgfd |& tee /tmp/t
dfgsdfgfd
sh-5.2$ cat /tmp/t
dfgsdfgfd
sh-5.2$

¯_(ツ)_/¯