LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 09-13-2012, 10:52 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
BASH:question about for i in {a..b}


Dear Experts,

With the code
Code:
#!/bin/bash
max=5
for i in {1..$max}
do
   echo "Welcome $i times"
done
I would expect for result like
Code:
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
However, in fact it printed out me
Code:
Welcome {1..5} times
How could I get correct multiple printout with variable such as $max in for i in {*..*} ?

I would thank for your help.

Last edited by cristalp; 09-13-2012 at 10:54 AM.
 
Old 09-13-2012, 12:26 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
The problem is that the brace expansion is done before the parameter expansion.
Code:
$ for i in "Welcome "{1..5}" times.";do echo ${i};done
Welcome 1 times.
Welcome 2 times.
Welcome 3 times.
Welcome 4 times.
Welcome 5 times.
$ j=5;for i in "Welcome "{1..$j}" times.";do echo ${i};done
Welcome {1..5} times.
<edit>
As a work-around, this might help:
Code:
#!/bin/bash
max=5
for i in $(echo "for (i=1;i<=${max};++i)i"|bc)
do
   echo "Welcome ${i} times"
done
which produces this:
Code:
$ bash tmp3
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
</edit>

Last edited by PTrenholme; 09-13-2012 at 02:46 PM.
 
Old 09-13-2012, 01:14 PM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Well, I tend to use seq for for loops. In fact I don't use a lot of bash structures, because I have no need of them (and they are confusing and I don't remember them).

Code:
bash-4.1$ j=5;for i in $(seq 1 $j);do echo Welcome $i times.;done
Welcome 1 times.
Welcome 2 times.
Welcome 3 times.
Welcome 4 times.
Welcome 5 times.
Probably someone will post how it should be done with expansion.

Last edited by H_TeXMeX_H; 09-13-2012 at 01:15 PM.
 
1 members found this post helpful.
Old 09-15-2012, 01:21 AM   #4
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by cristalp View Post
Dear Experts,

With the code
Code:
#!/bin/bash
max=5
for i in {1..$max}
do
   echo "Welcome $i times"
done
I would expect for result like
Code:
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
However, in fact it printed out me
Code:
Welcome {1..5} times
How could I get correct multiple printout with variable such as $max in for i in {*..*} ?

I would thank for your help.
Try with this:

Code:
max=5
for ((i=1;i<=max;i++))
do
   echo "Welcome $i times"
done
 
Old 09-16-2012, 04:17 AM   #5
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Rep: Reputation: 38
I don't think you can do this unless you use the alternative forms posted by the others.

Quoting from http://www.gnu.org/software/bash/man...xpansion.html: "Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result." So $max is evaluated only after the brace expansion is done.

You can see the difference here:

Code:
$ bash -x
...truncated...
$ max=5
+ max=5
$
$ for i in {1..$max}; do echo "Welcome $i times"; done
+ for i in '{1..$max}'
+ echo 'Welcome {1..5} times'
Welcome {1..5} times
$
$ for i in {1..5}; do echo "Welcome $i times"; done
+ for i in '{1..5}'
+ echo 'Welcome 1 times'
Welcome 1 times
+ for i in '{1..5}'
+ echo 'Welcome 2 times'
Welcome 2 times
+ for i in '{1..5}'
+ echo 'Welcome 3 times'
Welcome 3 times
+ for i in '{1..5}'
+ echo 'Welcome 4 times'
Welcome 4 times
+ for i in '{1..5}'
+ echo 'Welcome 5 times'
Welcome 5 times
$
I tried forcing max to be an int using declare -i max but that did not help either.
 
1 members found this post helpful.
Old 09-16-2012, 04:20 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
This is common Bash Pitfall #33

http://mywiki.wooledge.org/BashPitfa..._.7B1...24n.7D


Quite a few problems like this come down to the shell's parsing order. It really helps to clearly understand what happens before what when a command line is executed.

I've often thought it would be nice though if bash could implement a shell option to enable variable expansion inside brace expansion. It would make some coding situations much easier. But I suppose it would probably be kind of tricky to implement internally, due to the need to account for parameter expansion braces.

And nobody suggest using eval please, ok?


Incidentally, another option for printing simple sequences like this is to use seq's (at least the gnu version's) format option.

Code:
seq -f 'Welcome %g times.' "$n"
 
2 members found this post helpful.
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash question armandino101 Linux - General 6 04-24-2007 12:18 PM
another bash question. . downbound010 Programming 3 03-23-2006 03:37 AM
BASH question robscott Linux - Software 3 11-23-2005 09:39 AM
a bash question about while jiawj Linux - Newbie 3 07-14-2005 11:45 AM
bash question rjcrews Linux - Newbie 2 08-04-2004 09:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration