Register
It is currently Sat Nov 01, 2014 1:39 am

Confusion When Using Command Substitution


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon Feb 16, 2009 3:58 pm   

Joined: Mon Feb 16, 2009 3:43 pm
Posts: 2
Hello,

I just read through the Advanced Bash-Scripting Guide and was trying to play around with what I had learned. I came upon a behavior that I don't understand and I was hoping that somebody could help clear it up for me.
This is what happens:
Code:
$ echo number{$(seq -s , 3)}
number{1,2,3}

And this is what I was hoping would happen:
Code:
$ echo number{$(seq -s , 3)}
number1 number2 number3

This is a simple example, but I think it demonstrates my misunderstanding of how bash interprets the line. I would really appreciate it if someone could shed some light on this for me. Thanks.


Top
 Profile  
 PostPosted: Mon Feb 16, 2009 5:12 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Not really sure what the problem might be.

I'm getting mixed results here :P

Code:
sajko@hanna:~> bash <<EOF
sajko@hanna ~ $ echo number{$(seq -s , 3)}
sajko@hanna ~ $ EOF
number1 number2 number3

Code:
sajko@hanna:~> echo number{$(seq -s , 3)}
number{1,2,3}

Code:
sajko@hanna:~> ./test.sh
number{1,2,3}
sajko@hanna:~> cat test.sh
#!/bin/bash
echo number{$(seq -s , 3)}


Seems abit odd that it gets interpreted diffrently depending on execution frame.
The EOF one shouldn't do anything diffrently then the others.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Feb 17, 2009 1:06 am   

Joined: Mon Feb 16, 2009 3:43 pm
Posts: 2
Thanks for taking a look at it. I'll do some searching around and see if I can find out any more about this.


Top
 Profile  
 PostPosted: Tue Feb 17, 2009 10:28 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
This is interesting and I admit I didn't know why the variation in behavior, but I can guess :)

My bash man page says that Brace Expansion occurs before Command Substitution

I think that when run with the EOF the originating bash instance expands the seq command and the internal bash then does bracket expansion. In the other examples the command substitution happens AFTER it attempts (and fails due to incorrect syntax) the bracket expansion.

i hope I've made some sense here

Code:
[david@david rubix]# bash -c 'echo number{$(seq -s , 3)}'
number{1,2,3}
[david@david rubix]# bash -c "echo number{$(seq -s , 3)}"
number1 number2 number3


the quotes here seem to indicate that I'm at least on the right track :)


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 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:  


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