LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-04-2010, 08:12 PM   #1
ncalsmitty1369
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Rep: Reputation: 0
bash for loop iteration question.


Hi,

I am having a problem using the following for loop configuration in a bash shell:

array=( 1 2 3 )
num=${#array[*]}

for x in {1..$num}
do
echo $x
done

Instead of returning:
1
2
3
I get:
{1..3}

Can someone tell me what I am doing wrong?

Thanks!

Last edited by ncalsmitty1369; 02-05-2010 at 05:43 PM. Reason: solved
 
Old 02-04-2010, 08:58 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I don't know if your construct is **supposed** to work...if it is valid, it might need some quotes.

This works:

Code:
for x in $(seq 1 $num); do
    echo $x
done
 
Old 02-05-2010, 07:28 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,366

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Hi ncalsmitty1369 and welcome to LQ!

Answers to your question, here. And for the reasons explained in that thread, I second the suggestion given by pixellany.
 
Old 02-05-2010, 07:55 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by ncalsmitty1369 View Post
Hi,

I am having a problem using the following for loop configuration in a bash shell:

array=( 1 2 3 )
num=${#array[*]}

for x in {1..$num}
do
echo $x
done

Instead of returning:
1
2
3
I get:
{1..3}

Can someone tell me what I am doing wrong?

Thanks!
$num will not be expanded in brace expansion. you need to do eval
Code:
for i in $(eval echo {1..$num})
do
 echo $i
done
 
Old 02-05-2010, 05:50 PM   #5
ncalsmitty1369
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you all for the help!

Quote:
Originally Posted by colucix View Post
Hi ncalsmitty1369 and welcome to LQ!

Answers to your question, here. And for the reasons explained in that thread, I second the suggestion given by pixellany.
Thank you colucix for the welcome and the link to the other thread! Good information to know.

I like the solution provided by pixellany and will use if going forward.

Thanks to all!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Loop Question SoulShaker Linux - Server 5 06-17-2009 01:44 PM
python: can I see which iteration I'm on in a loop? BrianK Programming 2 08-27-2008 09:01 PM
Bash script Question while loop glennph93 Programming 6 05-25-2007 03:27 PM
BASH 'while' loop question GSMD Programming 4 04-13-2007 12:51 AM
Loop iteration in Linux scripting. 151803 Linux - Newbie 3 03-19-2007 06:36 AM


All times are GMT -5. The time now is 09:50 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration