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

Chet Ramey chet.ramey at case.edu
Sat May 23 07:41:58 PDT 2020


On 5/21/20 6:50 PM, Rob Landley wrote:

>>> 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.
> 
> Mine too, but grep isn't finding extglob under /etc or ~/.*

I can only suggest looking in your shell history.

> 
>>> $ 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.
> 
> ... they're not?
> 
> Why not?

Because aliases are expanded when tokens are read. You have to execute an
alias command before the alias becomes known, and you read and parse the
entire compound command before executing any of it. Just like shopt and
extglob.


>   $ bash -ic $'IFS=x; ABC=cxd; for i in +($ABC); do echo =$i=; done'
>   =+(c=
>   =d)=
>   $ strace bash -i -c 'exit' 2>&1 | grep open | wc -l
>   45
> 
> Is it one of those files, or did debian hack their bash source to change the
> builtin behavior? (Not going down this rathole right now.)

Maybe. But most of those opens are for the executables, their shared
libraries, and other mmaps.


> 
>   $ potato\
>   > (\
>   > )\
>   > { echo hello; }
> 
> Hmmm...
> 
>   $ potato\
>   > \
>   > (
>   bash: syntax error near unexpected token `newline'
> 
> You're right, it does. I'm not doing that.

There can't be any newlines between the word and the open paren. That's the
shell grammar.


> 
> So in bash, $LINENO isn't the starting line of a multi-line span, and it isn't
> the ending line of a multi-line span, it's the SECOND line of a multi-line span.

No. Consider one of the original examples:

echo $LINENO \
$LINENO

The shell has already recognized this as a simple command by the time it
sees the backslash-newline pair.

> I cannot come up with any way to explain that other than "implementation detail
> bubbling to the surface", and being unable to _document_ something has always
> made me reluctant to _implement_ it. Hmmm...

OK. Your implementation will be different since you're not using bison, so
it's ok that this corner case differs.


>>> 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've read most of that book.


> I first encountered Linux when the 4 SLS floppy images came across Fidonet in
> 1993, went over to a friend's house who had a computer to install it on (xfree86
> utterly refused to recognize his diamond stealth multimedia card and tried to
> come up in 320x200 resolution), played with the text mode a bit (no dos box?),
> went "why would anyone clone a sun workstation?" and wandered off again.

I was working, and we were a BSD/OS shop, mostly because we had been using
BSD for years and I knew all the CSRG guys. It was nice to be able to email
Keith Bostic and get unpuublished patches. Yes, I'm namedropping. We didn't
start using Linux for several years after that.

Universities are famous for cast-off hardware. I wrote a considerable
portion of bash-2.0 on an IBM RT in the basement of my first house running
a custom version of 4.3 BSD. I wrote a troff package to do APA style so my
wife could write her doctoral thesis on it.


> (When I was something like 12 I was told to use an experimental library catalog
> terminal that was going to replace the card catalogs, and I spaced off the end
> of the input field which put the terminal into an endless redraw loop they had
> to reboot the mainframe to stop. I got in trouble for that.)

Now that seems like a reaction due to embarrassment. It's not like the
developers could have done input validation or anything.


> Leaving posterity in a good position is sort of the point of the exercise, though:

Well, you hope to, at least.

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