[Toybox] Endless bash questions...
Chet Ramey
chet.ramey at case.edu
Mon Jun 6 08:49:12 PDT 2022
On 6/4/22 7:12 PM, Rob Landley wrote:
>
> P.S. Currently I'm trying to work out the sequencing of redirects vs
> $(subshells) because these work:
>
> $ bash -c $'if true; then cat; fi << EOF\none two\nEOF'
> one two
> $ bash -c $'[[ $(cat) == "one two" ]] <<EOF\none two\nEOF'
> $
> $ bash -c $'[[ $(cat) == "one two" ]] <<EOF\none two\nEOF\n[ $? -eq 0 ] && echo yes'
> yes
>
> But this hangs (cat is reading stdin, not the HERE document):
> $ bash -c $'[ $(cat) == "one two" ] <<EOF\none two\nEOF'
> ^C
It's an order of operations question. One of the things about compound
commands is that the redirections apply to the entire command, so they
are performed before the compound command is executed. So what happens is
the command gets parsed into a tree, like always, then executed, and
redirections happen before the command is executed. For `[[' the operand
expansion is part of the execution, so the redirection has already taken
place.
Now, `[' is a simple command, not a compound command. Bash performs the
word expansions before performing redirections. It always has, and it
makes things like handling null commands with redirections slightly
easier. It's not strictly Posix-conformant.
(And having the redirections precede the simple command on the input line
only affects the parser -- it ends up getting parsed to the same structure
and executed the same way as putting them after the command.)
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet at case.edu http://tiswww.cwru.edu/~chet/
More information about the Toybox
mailing list