Register
It is currently Sat Nov 01, 2014 3:44 am

regex question


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Sat Jan 22, 2011 5:19 am   

Joined: Sat Jan 22, 2011 5:12 am
Posts: 2
Hi!

Why does this work:
Code:
for i in db{1..30}; do echo $i; done


while this doesn't:
Code:
k=30; for i in db{1..$k}; do echo $i; done


/Nicke


Top
 Profile  
 PostPosted: Sat Jan 22, 2011 6:13 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
HI,

from man bash /EXPANSION /Brace Expansion :
Quote:
Brace expansion is performed before any other expansions[...]. Any incorrectly formed brace expansion is left unchanged.


Top
 Profile  
 PostPosted: Sat Jan 22, 2011 6:31 am   
User avatar

Joined: Sat Dec 11, 2010 5:27 pm
Posts: 15
Location: NSW, Australia
I don't understand what is wrong with
Code:
db{1..$k}
- the substitution is done producing {1..30} - I am a newbie to shell programming (so far)

I found that this works
Code:
k=30; for ((i=1; i<=$k; i++)); do echo $i; done


Alan


Top
 Profile  
 PostPosted: Sat Jan 22, 2011 6:48 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Like Watael is quoting, brace expansion is done before any other expansion.
So your shell tries to expand the {<num>..<num>}. Since the second num is not really a number, but the string '$k' it kan not really complete the expansion. Look at the lines below for an explanation
Code:
$> echo {1..10}
1 2 3 4 5 6 7 8 9 10

$> k=10; echo {1..$k}
{1..10}

In the second example your shell first expands {1..$k} which does not do anything and will leave it like it is. Next up is the variable substitution which will convert {1..$k} to {1..10}


Top
 Profile  
 PostPosted: Sat Jan 22, 2011 11:59 am   

Joined: Sat Jan 22, 2011 5:12 am
Posts: 2
Thanks for the answers! Why didn't I check 'man bash' ?? 8-}

man bash says, as pointed out earlier:
Quote:
The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.


/Nicke


Top
 Profile  
 PostPosted: Fri Mar 25, 2011 12:51 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
But, lo and behold:
Code:
k=10; eval echo {1..$k}


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP