Register
It is currently Fri Apr 18, 2014 4:39 am

Quoting in expanded variables


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Oct 09, 2009 2:30 pm   

Joined: Fri Oct 09, 2009 2:20 pm
Posts: 3
OK, bash newbie but seasoned programmer/developer. I'm trying to script a bash script to run m4 on a series of files, and I want to pass a bunch of macro definitions to m4 from a bash variable. Something like this:

PRODUCT=Product Name
...
M4MACROS="-D '_PRODUCT_=$PRODUCT' -D '_VERS_=$VERS' -D '_BETA_=$BETA'"
...
m4 ${M4MACROS} (more stuff)

As written above, this doesnt work. M4MACROS is expanded ok (as expected with the double quotes), but when ${M4MACROS} is expanded I hit a problem. Bash, of course, parses out the expansion splitting at spaces even though the individual macro names are in single quotes, which breaks up the macro definitions for m4 and messes everything up. I've looked at eval and other tricks but i'm scratching my head trying to think of a workaround here.

tia,
Tim


Top
 Profile  
 PostPosted: Sat Oct 10, 2009 5:10 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
have you tried escaping double quotes?

Code:
PRODUCT="product name"
M4MACROS="-D \"_PRODUCT_=$PRODUCT\" -D \"_VERS_=$VERS\" -D \"_BETA_=$BETA\""


I don't see why this shouldn't work, as always done that for me in that past :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Oct 12, 2009 2:39 pm   

Joined: Fri Oct 09, 2009 2:20 pm
Posts: 3
fredrik.eriksson wrote:
have you tried escaping double quotes?

Code:
PRODUCT="product name"
M4MACROS="-D \"_PRODUCT_=$PRODUCT\" -D \"_VERS_=$VERS\" -D \"_BETA_=$BETA\""


I don't see why this shouldn't work, as always done that for me in that past :)


Yeah I tried that .. it didnt work. So far as I can see there is no way do do this because BASH parses out the contents of variable expansion using $IFS separatores but IGNORES quoting. So you cannot use "late" quoting within a variable when the variable may contain (say) filenames with spaces in them.

I would find this extraordinary, but, to be frank, most of the bash syntax strikes me as arcane. And yes I'm aware it's inherited from sh etc, but that doesnt make it less arcane :)

--Tim


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

All times are UTC - 6 hours


Who is online

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