Please explain this script (from ABSG. Ch. 20):
exec 3>&1 # Save current "value" of stdout.
ls -l 2>&1 >&3 3>&- | grep bad 3>&- # Close fd 3 for 'grep' (but not 'ls').
# ^^^^ ^^^^
exec 3>&- # Now close it for the remainder of the script
Currently I imagine how it works:
- script connects fd/3 output to stdout
- pipeline connects stdout of
ls
to stdin ofgrep
(fd/3 is inherited by both processes) - redirerection of stderr of
ls
to stdout - stdout of
ls
is changed to fd/3 (sogrep
process has no morels
output, only errors) - fd/3 is closed for
ls
Is this the exactly sequence of redirections?
Can't understand why I see output of ls
if there are no errors and we close fd/3 where stdout is redirected.
What is the purpose of closing fd/3 for grep
process (grep bad **3>&-**
)?
ls
was replaced with unnamed pipe connected togrep
's stdin. So I could see output ofls
because its stdout was restored from fd/3 with redirection (>&3 3>&-).