LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] pass argument containing spaces (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-pass-argument-containing-spaces-4175420306/)

hashbang#! 08-04-2012 04:36 AM

[bash] pass argument containing spaces
 
In a shall script, I am building a parameter list for a later call:
Code:

opt="-option1 '60 0 50 0' -option2 ..."
...
convert $opt

problem: '60 0 50 0' needs to be passed as one parameter but it is split into four.


question: How can I force it to be recognized as one parameter?


set-up: GNU bash, version 4.2.20(1)-release (i486-pc-linux-gnu)

Chirel 08-04-2012 04:47 AM

Hi,

Code:

convert "$opt"

hashbang#! 08-04-2012 04:54 AM

Thank you Chirel, but this passes the WHOLE content of $opt as one parameter.

I just want '60 0 50 0' recognized as one parameter. My attempt to place it in single quotes does not do the trick.

What I need is:
Code:

{-option1}
{60 0 50 0}
{-option2}


414N 08-04-2012 05:03 AM

Are you really limited to bash-only?

grail 08-04-2012 09:39 AM

Go with an array:
Code:

opt=(-option1 '60 0 50 0' -option2 ...)

NevemTeve 08-04-2012 09:40 AM

Code:

eval convert $opt

hashbang#! 08-04-2012 12:46 PM

NevemTeve, eval was new to me and it does the trick.

grail: I am not quite sure what to do with the array:
Code:

$ convert ${opt[@]}
results in the parameters being split:
Code:

{-option1}
{60}
{0}
{50}
{0}
{-option2}

Thanks everyone for taking the time to respond. I certainly learnt something new.

grail 08-05-2012 08:32 AM

Well my understanding was that you wanted the individual items to stay grouped together:
Code:

opt=(-option1 '60 0 50 0' -option2 )

echo ${opt[0]}
-option1

echo ${opt[1]}
60 0 50 0

Not sure why you would use the @ which of course will display all items and when not quoted will suffer from word splitting.

hashbang#! 08-05-2012 08:58 AM

And how do I pass the array to convert?

Which Perth, by the way?

NevemTeve 08-05-2012 09:13 AM

convert "${opt[@]}" # untested

hashbang#! 08-05-2012 09:21 AM

That's what I described in post #7: it doesn't work; paramaters are split into words.

grail 08-05-2012 09:38 AM

Quote:

Which Perth, by the way?
Australia, not Scotland :(

Try adding quotes to the items required:
Code:

$ opt=(-option1 "'60 0 50 0'" -option2 )
$ echo "${opt[@]}"
-option1 '60 0 50 0' -option2

My original reading I thought you wanted to pass each item individually, ie not pass the entire line.

NevemTeve 08-05-2012 09:45 AM

Now I tested it, with quotes it does work:

Code:

$ opt=(-option1 '60 0 50 0' -option2 )
$ myecho -d -- ${opt[@]}
myecho -debug: 6 arguments:
  1: '-option1'
  2: '60'
  3: '0'
  4: '50'
  5: '0'
  6: '-option2'
$ myecho -d -- "${opt[@]}"
myecho -debug: 3 arguments:
  1: '-option1'
  2: '60 0 50 0'
  3: '-option2'


hashbang#! 08-05-2012 11:59 AM

OK, that's the array parameter passing sorted. Many thanks!

Quote:

Originally Posted by grail (Post 4746418)
Australia, not Scotland :(

At least you won't be eaten alive by the Scottish Highland midge!


All times are GMT -5. The time now is 06:33 AM.