So, it says right there that the correct way of piping standard error is:
find / -name passwd 2>&1 | less
It turns out that, in `man bash` it says that one can use:
find / -name passwd |& less
I find the latter easier to remember.
True ... the bottom is much easier to remember. One thing to keep in mind for courses is the difference between "best practices" and what actually works from a technical perspective. I often point people to man pages for examples and for copy/paste/tweak/execute so you don't need to worry about remembering all the syntax and options for things like exams.
I think the reason the book mentions the top method is because each type of stream has meanings ...
STDIN / STDOUT / STDERR
While yes, the second method works, you can't 100% clearly know what is happening without something like the man page and knowing BASH. The book is trying to teach you the different processes and channels and how those work with redirection of output and other things like pipe.
There are always multiple ways to do things and I would agree, I almost never use the way you mention in #1 (except when I've had to teach the RHCSA course).
However, knowing these ways and the tee command and other things will be beneficial as you will encounter official documentation telling you to type the command so it is very important to know and understand what it means.
Good take on it. I think it should be mentioned at least.
"2>&1 |" was the old old original syntax, from the original Thompson shell in the early 70's if I remember right. It was retained for backward compatibility, but many people found it difficult to remember. So they added "|&" much more recently -- I believe in the Bourne shell.
Since it was added in Bourne shell, be careful with your shbang lines. If you start a script with "#!/bin/sh", that script will run with the /bin/sh shell. On RHEL /bin/sh is just a link to /bin/bash, so |& works fine. But some older flavors of Unix run the actual Thompson /bin/sh, so |& (among other things) won't work.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.