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.
18 Comments
racketlauncher831@lemmy.ml · 9 pts · 1y
As true as this can be, it's counterintuitive.
exu@feditown.com · 8 pts · 1y
I never use more than one redirection to keep it simple. Stuff gets even weirder when you also use
<in the same linesMonkderVierte@lemmy.zip · 1 pts · 1y
envsubstis a exception.korbel@lemmy.ml · 3 pts · 1y
In bash if you want to redirect both stderr and stdout to file you can use
&>filename.INeedMana@piefed.zip · 3 pts · 1y
IMO
|& tee dirlistis easier to managelousyd@lemmy.sdf.org · 4 pts · 1y
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 pts · 1y
For
2>&1to work don't we need to be using some shell anyway?lousyd@lemmy.sdf.org · 2 pts · 1y
Yeah. The shell, plus whatever command (or commands) you tell the shell to run.
MonkderVierte@lemmy.zip · 1 pts · 1y
This is a Bash thing?
INeedMana@piefed.zip · 1 pts · 1y
https://www.man7.org/linux/man-pages/man1/bash.1.html
MonkderVierte@lemmy.zip · 1 pts · 1y
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 pts · 1y
True
But nowadays
/bin/shis often just a link to bashMonkderVierte@lemmy.zip · 1 pts · 1y
Which puts Bash in POSIX compliance mode.
INeedMana@piefed.zip · 2 pts · 1y
¯_(ツ)_/¯
the_crotch@sh.itjust.works · 2 pts · 1y
What distro uses /Users instead of /home? Doesn't that break the standard?
samc@feddit.uk · 9 pts · 1y
MacOS
the_crotch@sh.itjust.works · 2 pts · 1y
I used macs for 15 years I really should have known that lol.
lousyd@lemmy.sdf.org · 1 pts · 1y
Playskool