RenichBon
Mission Specialist
Mission Specialist
  • 659 Views

RJ124 ch05: About redirection

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.

0 Kudos
3 Replies
Travis
Moderator
Moderator
  • 635 Views

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

Travis_0-1695481831699.png

 

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. 

Travis_1-1695481945881.png

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.

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training
RenichBon
Mission Specialist
Mission Specialist
  • 586 Views

Good take on it. I think it should be mentioned at least.

0 Kudos
GaryF
Cadet
Cadet
  • 556 Views

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

0 Kudos
Join the discussion
You must log in to join this conversation.