On Mon, May 01, 2017 at 04:37:10PM +0200, Florian Mayer wrote:
$ "{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.
No, and no. I think you actually meant to write that without the outer
double quotes.
imadev:~$ set -x
imadev:~$ {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: command not found
It most certainly does not evaluate to 55.
With the outer double quotes, you simply get:
imadev:~$ "{1..10}'+ 0'"
+ '{1..10}'\''+ 0'\'''
bash: {1..10}'+ 0': command not found
But doesn't this case show that bash choses the preamble and postable
for the brace expansion wrongly?
No, it shows that you are wishing for a world that does not exist.
Your understanding of what bash does is not yet complete.
Gods save me from friggin' LOBBYISTS.
why can't we just brace expand everything in between (( ))
as if we'h read it verbatimly from the commandline?
Why can't we just write a loop?
In any case where this brace expansion insanity would actually *work*
(result in the correct answer, without breaking the script, or your
system's security, or consuming outrageous amounts of memory), you
could simply write out the literal expression longhand, or simply write
out the *answer*.
You want to print "55"? echo 55
In any case where the expression is too long to write out longhand,
writing a generator to produce a script that contains the longhand
expression, and then evaluating that script, leads to one of the problems
I mentioned earlier. Even if you are the Brace Whisperer and you somehow
manage to dodge EVERY security issue involved with eval, you still have
the "uses too much memory" problem. The results of a brace expansion
are actually generated as words stored in memory. It's not efficient.
It's not elegant. It's ridiculous and stupid.