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

Chet Ramey chet.ramey at case.edu
Wed May 20 06:39:58 PDT 2020


On 5/19/20 4:03 PM, Rob Landley wrote:
> On 5/18/20 10:41 AM, Chet Ramey wrote:
>> On 5/17/20 7:11 AM, Rob Landley wrote:
>>> I had a reply window open to this when my laptop battery died, and thunderbird
>>> doesn't store unfinished messages like kmail and vi and chrome...
>>>
>>> Anyway, I was reminded of this thread by:
>>>
>>>   $ IFS=x; ABC=cxd; for i in +($ABC); do echo =$i=; done
>>>   =+(c=
>>>   =d)=
>>>   $ bash -c 'IFS=x; ABC=cxd; for i in +($ABC); do echo =$i=; done'
>>>   bash: -c: line 0: syntax error near unexpected token `('
>>>   bash: -c: line 0: `IFS=x; ABC=cxd; for i in +($ABC); do echo =$i=; done'
>>>   $ readlink -f /proc/$$/exe
>>>   /bin/bash
>>
>> Yes, you need extglob to get a word like +(xyz) to parse unquoted. Since
>> the word list following `in' is subject to pathname expansion, it's valid
>> in that context.
> 
> But the same command line in the current shell and in bash -c are parsing
> different despite presumably reading the same .bashrc and friends?

My guess is that the interactive shell has extglob enabled somehow, whether
it was in .bashrc or interactively after it was started. The -c command
shell doesn't read startup files, so that doesn't matter.


> $ bash -c 'shopt -s extglob; IFS=x; ABC=cxd; for i in +($ABC); do echo =$i=; done'
> bash: -c: line 0: syntax error near unexpected token `('
> bash: -c: line 0: `shopt -s extglob; IFS=x; ABC=cxd; for i in +($ABC); do echo
> =$i=; done'
> 
> Nope, that wasn't it either.

This is the same thing that keeps aliases from being defined and expanded
in the same compound command. Enabling extglob changes the behavior of the
shell parser. Since the shell always reads at least one line of input, and
parses all of the commands on that line before executing any of them, the
shopt will not be executed before the shell reads +($ABC).



>> No. Kind of in the middle. Consider the following:
>>
>> echo \
>> one \
>> two \
>> $LINENO \
>> done
>>
>> Bash will always expand $LINENO to 2 in this construct, since it was on
>> line 2 when it figured out it was parsing a simple command.
> 
> *blink* *blink.
> 
> Ok, that just makes NO sense to me.

This is 100% what we talked about last week. The parser has to look ahead
to determine whether or not it's parsing a function definition, and by the
time it figures that out (after it gets the `one' from the lexer), we're
on the second line. That's the line number that gets attached to the one-
line simple command.

>> You fell victim to one of the classic blunders.
> 
> I have neither gotten involved in a land war in asia nor gone up against a
> sicilian when death is on the line (although the second one appears negotiable).

The second is only slightly less well-known.


> Yeah, I wrote my own parsing plumbing:
> 
>   https://github.com/landley/toybox/blob/master/lib/args.c#L74
> 
> It gets called automatically before each COMMAND_main() function, so the options
> are already parsed and the results waiting before the command-specific C logic
> starts running.

That looks clever.

> Google/alphabet/android itself has given me the Google Open Source award twice,
> each time with a $200 payoneer gift card (I spent the first one on japanese
> lessons and an online game, the second needs some website activation thing that
> didn't work, I should try again someday). 

I got one of those. It was for Google merch, though. :-) I still have some
of it.

> Beyond that... Elliott bought me lunch once, and Tim Bird of Sony paid my travel
> and hotel to ELC one year (but I think that might have been back when I was
> doing busybox, not toybox? 

The FSF bought me a computer to do development on after shellshock. It's
not all bad.
> That's the only place it's freely published. There were stupid "pay me for
> printouts" versions

There still are.

> That's the one they set up when they got kicked off of comp.os.minix when
> Tanenbaum came back for the new semester and started objecting to the traffic:
> 
>   https://www.oreilly.com/openbook/opensources/book/appa.html
> 
> Fascinating stuff, if you have a computer history hobby like I do. :)

Yep, I was there for a lot of that.

>>> I only started using bash in 1998. :)
>>
>> And it was 10 years old at that time. Man, we've come a long way.
> 
> I started toybox in 2006. I'm old...

We're both old.

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