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

Re: bash expansion crap...



On Tue, Oct 02, 2007 at 11:49:16AM -0700, Andrew Sackville-West wrote:
> andrew@basement:~$ for i in {1..3}; do echo $i; done
> 1
> 2
> 3

I don't get that result (you did say 'bash', right?):

    ken@moffit:~/ 0$  for i in {1..3}; do echo $i; done
    {1..3}

Hmm... that was on a sarge system, with bash 2.05b-26; on a sid box,
bash 3.1dfsg-8, I do get your result.

> andrew@basement:~$ TEST=3; for i in {1..$TEST}; do echo $i; done
> {1..3}
> 
> 
> in the first example, its obvious. In the second, $TEST gets replaced
> with 3, but then the {} doesn't get expanded. I'm sure I have to do
> some kind of wacky $({[ type thing, but I'm not able to grok it. any
> ideas? 

I don't see the {1..3} construct in bash(1), so not sure how it's working
for you in the first place.  Brace expansion is a different animal,
not applicable to what you're doing.  But that was on sarge... On sid,
the construct is described (26% into the manpage):

   A sequence expression takes the form {x..y}, where x and y are either integers
   or single characters.  When integers are supplied, the expression  expands  to
   each  number  between  x  and y, inclusive.  When characters are supplied, the
   expression expands to each character lexicographically between x and y, inclu-
   sive.  Note that both x and y must be of the same type.

I think this is to taken at face value, and the parameters have to be
integers (or characters), and not variables which would expand to same.
I'm not a bash basher, but the language has its share of quirks (and
then some).  I've been pushing at it quite a bit, though, and there's
a lot you can get done using bash.

> the purpose is to be able to easily update some loops in a script for
> different numbers of object to iterate over. obvious, I guess.

Maybe the for loop would be appropriate?

   for (( expr1 ; expr2 ; expr3 )) ; do list ; done
      First,  the  arithmetic  expression expr1 is evaluated according to the
      rules described below  under  ARITHMETIC  EVALUATION.   The  arithmetic
      ...

Ken

-- 
Ken Irving, fnkci+debianuser@uaf.edu



Reply to: