bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Clarification - Space removal in AE takes place before brace expansi


From: Chet Ramey
Subject: Re: Clarification - Space removal in AE takes place before brace expansion
Date: Mon, 1 May 2017 15:00:58 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.0.1

On 5/1/17 10:37 AM, Florian Mayer wrote:
> 
>> OK.  This starts out as one word: "{1..10}'+' +0". Brace expansion
> Ok, that's obviously a dumb mistake. And everything you wrote is right.
> 
> Just to clean up the mistake:
> $ (( {1..10}'+' 0)) # spaces before { and 0
> 
> So, as you described, what bash actually does is it takes
> $ (( {1..10}'+' 0))
> goes into arithmetic expression context and performes brace
> expansion by chosing {1..10} as the amble and "'+' 0" as postscript

Not exactly, unless what you mean by "arithmetic expression context" is
that the characters between the open and close parens are read as a single
word.

> So
> $ "{1..10}'+' 0"
> actually is seen as
> $ "{1..10}'+ 0'" # postabmle = '+<space>0'

Close. The quotes are still there. They don't get removed until quote
removal.

> Then, it expands that to
> '1+ 0 2+ 0 3+ 0 4+ 0 5+ 0 6+ 0 7+ 0 8+ 0 9+ 0 10+ 0'
> and evaluates it arithmetically resulting in 55.

It doesn't result in 55; it's an arithmetic syntax error. `0 2' is not a
valid arithmetic constant.

>> The whitespace discrepancy might be caused by the fact that brace
>> expansion generates multiple words, which have to be rejoined into a
>> single string.
> But doesn't this case show that bash choses the preamble and postable
> for the brace expansion wrongly?

Not at all. The contents of an arithmetic command are parsed as a single
string, and brace expansion behaves as it does with any other string.
You might want the expression to be tokenized, but that's not what you're
saying.

> 
>> word (the space between the `+ +') is preserved.
> 
> Why is it preserved?

Because it's part of the same word.

> Were I to do
> $ echo {1..10}'+' 0
> I'd get a completely other set of words: '1+ 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+ 10+ 0'

Sure. You have two words, not one.  Brace expansion happens on the first
word.  (Well, it happens visibly on the first word. It's performed on the
`0' as well, but has no effect.)

> I don't know how arithmetic expansion works, but
> why can't we just brace expand everything in between (( ))
> as if we'h read it verbatimly from the commandline?

What does this mean? I assume it means you'd like bash to tokenize the
text between the (( and )). How does that help anyone except for this
particular edge case? You still have to join the tokens into a single
string to be evaluated.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]