LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-04-2012, 05:36 AM   #1
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Rep: Reputation: 17
[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)
 
Old 08-04-2012, 05:47 AM   #2
Chirel
Member
 
Registered: Nov 2009
Posts: 55

Rep: Reputation: 19
Hi,

Code:
convert "$opt"
 
Old 08-04-2012, 05:54 AM   #3
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
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}
 
Old 08-04-2012, 06:03 AM   #4
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
Are you really limited to bash-only?

Last edited by 414N; 08-04-2012 at 07:14 AM.
 
Old 08-04-2012, 10:39 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Go with an array:
Code:
opt=(-option1 '60 0 50 0' -option2 ...)
 
1 members found this post helpful.
Old 08-04-2012, 10:40 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,947

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Code:
eval convert $opt
 
1 members found this post helpful.
Old 08-04-2012, 01:46 PM   #7
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
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.
 
Old 08-05-2012, 09:32 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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.
 
Old 08-05-2012, 09:58 AM   #9
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
And how do I pass the array to convert?

Which Perth, by the way?
 
Old 08-05-2012, 10:13 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,947

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
convert "${opt[@]}" # untested
 
Old 08-05-2012, 10:21 AM   #11
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
That's what I described in post #7: it doesn't work; paramaters are split into words.
 
Old 08-05-2012, 10:38 AM   #12
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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.
 
1 members found this post helpful.
Old 08-05-2012, 10:45 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,947

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
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'
 
2 members found this post helpful.
Old 08-05-2012, 12:59 PM   #14
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
OK, that's the array parameter passing sorted. Many thanks!

Quote:
Originally Posted by grail View Post
Australia, not Scotland
At least you won't be eaten alive by the Scottish Highland midge!
 
  


Reply

Tags
bash, parameters


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To pass argument in Expect command? jprathap Linux - Newbie 3 08-27-2009 05:58 PM
Pass argument to module in grub Racuel Linux - Kernel 1 04-21-2009 11:27 PM
Possible to pass an argument from one shell env to another? ne00 Programming 4 04-15-2009 10:19 PM
bash pass multiple arguments with spaces msreddy999 Linux - Software 10 04-07-2009 05:38 PM
How to pass an argument to DHCPCD ? shazam75 Linux - Networking 3 02-27-2005 11:34 AM


All times are GMT -5. The time now is 01:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration