Register
It is currently Fri Aug 01, 2014 6:23 pm

Quoting inside strings, multiple expansion


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Dec 04, 2013 2:55 pm   

Joined: Wed Dec 04, 2013 2:29 pm
Posts: 2
Dear all.

I am trying to execute a command from a bash script, which requires quoted argument like this:
Code:
executable -option1 value1 -option2 "value2 value3 value4" -option3 value3


If possible, i would like to build a string, which could be passed as single variable; something like:
Code:
op2='-option2 "value2 value3 value4"'
parameters="-option1 value1 $op2"

and then run the command like:
Code:
executable $parameters


However, I am no able to achieve this.
Thank you for your advice(s).


Top
 Profile  
 PostPosted: Wed Dec 04, 2013 6:11 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Since you're not quoting parameters variable's expansion there:
Code:
executable $parameters

the resulted string from the expansion undergoes word splitting.


What does it mean? Well, parameters variable is defined as follow:
Code:
parameters="-option1 value1 $op2"

with the result expansion of variable op2 which is defined like this:
Code:
op2='-option2 "value2 value3 value4"'


When bash(1) sees the following unquoted:
Code:
$parameters

it replaces this string with the actual value of the variable parameters, then
since the expansion is unquoted, the whole string resulting from it gets split
into multiple words according to each character presents in the value of the
the shell variable IFS, that defaults to <space><tab><newline>.

So to recap the parsing steps quickly:
Code:
1) executable $parameters
2) executable -option1 value1 $op2 # op2 is actually expanded when `parameters' gets defined but it's here to illustrate
3) executable -option1 value1 -option2 '"value2' value3 'value4"' # word splitting on space

As you can see, this is not what you want, hence it doesn't behave like you want it to. (« not working »)

After reading that explanation, you might now decide to quote « $parameters », but
doing so would result in the string being passed to executable as only one argument.

So that won't help, because you want each word to be expanded
separately to make executable see multiple arguments, not one.


The best workaround to this in bash(1) is the use of array variables.

Thanks to them, you can store multiple elements in one variable, that,
when expanded using the @ pattern - in a quoted variable expansion -
will cause all elements to be expanded separately, resulting in multiple
quoted words that won't get word split because, again, they get quoted.

Code:
op2=(-option2 "value2 value3 value4")
parameters=(-option1 value1 "${op2[@]}")

executable "${parameters[@]}"


Top
 Profile  
 PostPosted: Sat Dec 07, 2013 11:44 am   

Joined: Wed Dec 04, 2013 2:29 pm
Posts: 2
Thank you, this was very helpful.


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: Bing [Bot], Google [Bot] 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:  


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