[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Clarification - Space removal in AE takes place before brace expansi
From: |
Florian Mayer |
Subject: |
Re: Clarification - Space removal in AE takes place before brace expansion |
Date: |
Mon, 01 May 2017 16:37:10 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 |
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
So
$ "{1..10}'+' 0"
actually is seen as
$ "{1..10}'+ 0'" # postabmle = '+<space>0'
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.
When I do
$ set -x
I get:
$ (( {1..10}'+' 0))
$ + (( 1+ 0 2+ 0 3+ 0 4+ 0 5+ 0 6+ 0 7+ 0 8+ 0 9+ 0 10+ 0 ))
$ bash: ((: 1+ 0 2+ 0 3+ 0 4+ 0 5+ 0 6+ 0 7+ 0 8+ 0 9+ 0 10+ 0:
syntax error in expression (error token is "2+ 0 3+ 0 4+ 0 5+ 0 6+ 0
7+ 0 8+ 0 9+ 0 10+ 0")
Which shows the exact same pattern.
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?
word (the space between the `+ +') is preserved.
Why is it preserved?
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'
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?
Re: Clarification - Space removal in AE takes place before brace expansion, Chet Ramey, 2017/05/01