|
Home > Archive > Unix Shell > January 2006 > Why double paranthesis in for loop?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Why double paranthesis in for loop?
|
|
|
| All, in the alternate form of the for loop, why do we need a double
paranthesis? What's the rationale behind this?
Ex:
for ((i=0; i<5; i++)); do echo $i; done
TIA,
Sashi
| |
| Xicheng 2006-01-29, 9:31 pm |
| In bash, we use $((...)) to evaluate arithmetic condition, ((...)) is
very similar to $((...)), but neater and more efficient.
Xicheng
Sashi wrote:
> All, in the alternate form of the for loop, why do we need a double
> paranthesis? What's the rationale behind this?
> Ex:
> for ((i=0; i<5; i++)); do echo $i; done
> TIA,
> Sashi
| |
| Janis Papanagnou 2006-01-29, 9:31 pm |
| Sashi wrote:
> All, in the alternate form of the for loop, why do we need a double
> paranthesis? What's the rationale behind this?
> Ex:
> for ((i=0; i<5; i++)); do echo $i; done
> TIA,
> Sashi
>
You need some/any syntax. Given that originally there was only the
for arg in list
a consistent extension could as well have have been
for arg from -10 to 10 step 1
without brackets at all, or
for arg in {-10..10..1}
to combine the for loop with a new ksh93r feature.
But since there already are single parenthesis used for subshells,
and there are already double parenthesis in arithmetic expansion
$((...)), which is also POSIX, and the arithmetic command ((...)),
it might have been an apparent choice to use double quotes also
for this kind of arithmetic in a loop.
Janis
| |
| Stephane CHAZELAS 2006-01-29, 9:31 pm |
| 2006-01-26, 12:01(-08), Sashi:
> All, in the alternate form of the for loop, why do we need a double
> paranthesis? What's the rationale behind this?
> Ex:
> for ((i=0; i<5; i++)); do echo $i; done
[...]
((...)) generally introduces an arithemtic expression, while
(...) introduces a subshell as in (subshell) or $(command
substitution), and is also used in array assignments:
var=(...)
for ((...))
makes sense when you think of
while ((i < 5))
though one can use ((i < 5)) on its own (outside a while loop)
while you can't use ((...;...;...)) outside of a for loop.
zsh also has the shorter for loop form:
for i (a b c) do-something-with $i
that can be used as:
for i ({0..5}) print $i
for instance.
(shorter as print -l {0..5})
Remember zsh is intended to be a shell and to be used as such
(at the prompt), so size (when small) matters.
--
Stéphane
| |
| Stephane CHAZELAS 2006-01-29, 9:31 pm |
| 2006-01-26, 21:33(+01), Janis Papanagnou:
> Sashi wrote:
>
> You need some/any syntax. Given that originally there was only the
>
> for arg in list
>
> a consistent extension could as well have have been
>
> for arg from -10 to 10 step 1
>
> without brackets at all, or
>
> for arg in {-10..10..1}
>
> to combine the for loop with a new ksh93r feature.
[...]
That's a zsh feature that was brought to bash and then to ksh93
(though it must come from PERL in the first place).
--
Stéphane
| |
| William Park 2006-01-29, 9:31 pm |
| Stephane CHAZELAS <this.address@is.invalid> wrote:
> 2006-01-26, 21:33(+01), Janis Papanagnou:
> [...]
>
> That's a zsh feature that was brought to bash and then to ksh93
> (though it must come from PERL in the first place).
Which one is Zsh feature? If you're talking about {..}, then that would
Fortran, if I remember.
--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
| |
| Kurt Swanson 2006-01-29, 9:31 pm |
| Stephane CHAZELAS <this.address@is.invalid> writes:
> 2006-01-26, 21:33(+01), Janis Papanagnou:
[vbcol=seagreen]
> [...]
> That's a zsh feature that was brought to bash and then to ksh93
> (though it must come from PERL in the first place).
[Again showing my age...]
Looks like Pascal to me...
--
© 2005 Kurt Swanson AB
| |
| Janis Papanagnou 2006-01-29, 9:31 pm |
| Kurt Swanson wrote:
> Stephane CHAZELAS <this.address@is.invalid> writes:
>
>
>
> [Again showing my age...]
>
> Looks like Pascal to me...
Not quite; maybe you're mixing two different Pascal constructs, which
would have been...
for arg := -10 to 10 do
and
if arg in [-10..10] then
....as far as memory serves.
Janis [not hiding his age either] ;-)
| |
| Stephane CHAZELAS 2006-01-29, 9:31 pm |
| 2006-01-26, 17:12(-05), William Park:
> Stephane CHAZELAS <this.address@is.invalid> wrote:
>
> Which one is Zsh feature? If you're talking about {..}, then that would
> Fortran, if I remember.
The {1..5} as brace expansion (brace expansion was introduced by
csh, foo{1..5} is an extension that expands to 5 words foo1,
foo2... foo5) was first introduced in zsh. I don't think it would
make any sense in a low-level compile language such as fortran
or pascal. Even in perl, {1..5} expands to a list of integers,
that is not really like shells brace expansion.
Fortran (1954) predates Pascal (1970), btw.
--
Stéphane
|
|
|
|
|