[Toybox] Would someone please explain what bash is doing here?

Chet Ramey chet.ramey at case.edu
Thu Mar 12 07:37:24 PDT 2020


On 3/11/20 11:55 PM, Rob Landley wrote:

> The bash man page says:
> 
>   There are seven kinds of expansion performed: brace expansion,
>   tilde expansion, parameter and variable  expansion,  command  substitu‐
>   tion, arithmetic expansion, word splitting, and pathname expansion.
> 
> Except in reality there's sort of eight (quote removal, which << EOF does by
> itself, but here is done as part of variable expansion).

Yeah, it kind of is one of the expansions, and POSIX described it as such,
but bash describes it as something that happens after the rest of the word
expansions.

> 
> I've implemented MOST of brace expansion (need to go back and do {1..2..3},
> tilde expansion, about half of variable expansion (next up the ${blah} can of
> worms with ${blah/a/b} and ${#blah} and ${blah:-x} which is SLIGHTLY DIFFERENT
> than ${blah-x}...). I've done command substitution but need more debugging
> scrutiny on it (it worked, then I broke it, then it worked again...), haven't
> started arithmetic expansion yet (gotta write one of them operand stack +
> operator stack thingies with priorities, which is fun because it writes to
> variables that don't exist yet AND recursively resolves stuff:
> 
>   $ fruit=basket
>   $ potato=fruit
>   $ basket=42
>   $ echo $((potato))
>   42

Yes, the value of a variable is treated as an expression instead of a
constant and recursively evaluated.


> Word splitting isn't really a seperate step, exactly? "echo one two three"
> doesn't care about $IFS, and the output of any of the OTHER substitutions:

It is, because it operates on the output of the other expansions. Like you
said, that echo command doesn't undergo word splitting at all.

> And then pathname expansion. Haven't started it yet. I have dirtree and friends,
> and I wrote my own globbing logic from scratch once back in the 90's. Not too
> worried about it, just... a bit to chew between here and there.

Most C libraries have a glob() and fnmatch() you can start with.

-- 
``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