[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why does case-pattern undergo "process substitution"?
From: |
Chet Ramey |
Subject: |
Re: Why does case-pattern undergo "process substitution"? |
Date: |
Mon, 26 Aug 2024 11:18:46 -0400 |
User-agent: |
Mozilla Thunderbird |
On 8/23/24 3:41 PM, shynur . wrote:
Hi, friends~
I'm reading the Bash Reference Manual, section 3.2.5.2.
Here: <https://gnu.org/s/bash/manual/html_node/Conditional-Constructs.html>.
case _word_ in
[ [(] _pattern_ [| _pattern_]…) _command-list_ ;;]…
esac
Each _pattern_ undergoes tilde expansion, parameter
expansion, command substitution, arithmetic expansion,
process substitution, and quote removal.
^^^^^^^^^^^^^^^^^^^^
I'm confused. Why is "process substitution" being performed here?
It's performed because it's one of the standard shell word expansions.
POSIX specifies that the word and pattern undergo all the word
expansions except word splitting, filename generation, and quote removal
(for the pattern, not the word, and that has sparked some discussion),
so bash does the same -- it happens to have more expansions than POSIX.
The result is often unforeseen, e.g., '/dev/fd/13', '/dev/fd/42'...
Can anyone give me a practical example? Thanks in advance!
Expanding the word part is easy: since process substitution expands to
a filename, one can infer the underlying implementation from the particular
filename:
case <(:) in
/dev/fd*) echo process substitution uses anonymous pipes;;
*sh-np*) echo process substitution uses named pipes;;
esac
Some scripts make use of this information. The pattern should undergo the
same expansions as the word. If you don't want process substitution to
occur, you can always quote it.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/
OpenPGP_signature.asc
Description: OpenPGP digital signature