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

Rob Landley rob at landley.net
Sat May 23 10:06:28 PDT 2020


On 5/23/20 9:41 AM, Chet Ramey wrote:
> 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.

It's there in each new terminal tab. It's not a thing I typed.

>>>> $ 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.

... can aliases expand to unbalanced ( or { ?

  $ alias fluglehorn='echo $('
  $ fluglehorn echo hello)
  hello

Oh lovely. That means there can be redirects in there.

  $ alias parkingmeter='echo potato >'
  $ parkingmeter stomp.txt
  $ cat stomp.txt
  potato

Yes there can.

>>   $ 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.

But wasn't \ supposed to eat the newline?

  $ echo -n abc\
  > def | wc -c
  6

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

  $ echo $LINENO \
  > $LINENO \
  > $LINENO
  1 1 1

  $ bash -c $'echo $LINENO \\\n$LINENO'
  0 0

Wait, why's it not doing it now?

I'm going to step away from this until I've had more caffeine.

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

Not if existing scripts depend on this behavior. (In this case probably not, I
think $LINENO is mostly a debugging aid.)

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

Did you actually get to use IBM Academic OS?

There was this whole "everybody used BSD in 1980-1984, then AT&T paid them to
switch to System V" thing that isn't sufficiently documented, and I've never
figured out if AOS->AIX is part of that or just IBM hadn't gotten into unix yet.
(The prominent example is SunOS->Slowaris but there were plenty of others.)

Rob



More information about the Toybox mailing list