the order of redirections is significant
from lousyd@lemmy.sdf.org to linux@lemmy.ml on 23 Jul 20:13
https://lemmy.sdf.org/post/39101854

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.

#linux

threaded - newest

exu@feditown.com on 23 Jul 20:44 next collapse

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 on 24 Jul 09:39 collapse

envsubst is a exception.

INeedMana@piefed.zip on 23 Jul 20:49 next collapse

IMO |& tee dirlist is easier to manage

lousyd@lemmy.sdf.org on 23 Jul 20:50 next collapse

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 on 23 Jul 21:01 collapse

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

lousyd@lemmy.sdf.org on 23 Jul 22:04 collapse

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

MonkderVierte@lemmy.zip on 24 Jul 09:35 collapse

This is a Bash thing?

INeedMana@piefed.zip on 24 Jul 09:56 collapse

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 on 24 Jul 10:11 collapse

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 on 24 Jul 10:25 collapse

True

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

MonkderVierte@lemmy.zip on 24 Jul 10:27 collapse

Which puts Bash in POSIX compliance mode.

INeedMana@piefed.zip on 24 Jul 11:30 collapse

$ sh
sh-5.2$ echo dfgsdfgfd |& tee /tmp/t
dfgsdfgfd
sh-5.2$ cat /tmp/t
dfgsdfgfd
sh-5.2$

¯_(ツ)_/¯

racketlauncher831@lemmy.ml on 24 Jul 08:54 next collapse

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

AndrasKrigare@beehaw.org on 24 Jul 11:33 collapse

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.

korbel@lemmy.ml on 24 Jul 12:05 next collapse

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

the_crotch@sh.itjust.works on 24 Jul 20:38 collapse

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

samc@feddit.uk on 24 Jul 21:08 next collapse

MacOS

the_crotch@sh.itjust.works on 24 Jul 23:47 collapse

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

lousyd@lemmy.sdf.org on 24 Jul 21:24 collapse

Playskool