[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to use variable in a range like {a..b}?
From: |
Greg Wooledge |
Subject: |
Re: How to use variable in a range like {a..b}? |
Date: |
Wed, 12 May 2010 08:20:36 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Wed, May 12, 2010 at 06:27:43AM -0500, Peng Yu wrote:
> x=10
> for i in {1..$x}; do echo $i; done
That won't work because of the way bash does its parsing. Brace expansion
is done before parameter (variable) expansion. Since the brace-expansion
part doesn't see 1..10 it can't generate a list of integers.
> I'm wondering how to use variable in a range?
for ((i=1; i<=x; i++)); do ...
... is the way I'd do it.
On Wed, May 12, 2010 at 01:57:35PM +0200, Roman Rakus wrote:
> This works for me;
> x=10; for i in $(eval echo {1..$x}); do echo $i; done
> But is not so cute.
There's also the more traditional (and POSIX-compatible):
x=10; i=1; while [ $i -le $x ]; do ...; i=$(($i+1)); done