Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a complete argument list in bash while keeping mulitword arguments together?

Tags:

linux

bash

I am having some issues with word-splitting in bash variable expansion. I want to be able to store an argument list in a variable and run it, but any quoted multiword arguments aren't evaluating how I expected them to.

I'll explain my problem with an example. Lets say I had a function decho that printed each positional parameter on it's own line:

#!/bin/bash -u
while [ $# -gt 0 ]; do
  echo $1
  shift
done

Ok, if I go decho a b "c d" I get:

[~]$ decho a b "c d"
a
b
c d

Which is what I expect and want. But on the other hand if I get the arguments list from a variable I get this:

[~]$ args='a b "c d"'
[~]$ decho $args
a
b
"c
d"

Which is not what I want. I can go:

[~]$ echo decho $args | bash
a
b
c d

But that seems a little clunky. Is there a better way to make the expansion of $args in decho $args be word-split the way I expected?

like image 652
David Dean Avatar asked Feb 18 '09 13:02

David Dean


2 Answers

You can use:

eval decho $args
like image 171
mouviciel Avatar answered Oct 31 '22 16:10

mouviciel


You can move the eval inside the script:

#!/bin/bash -u
eval set -- $*
for i; 
do 
  echo $i;
done

Now you can do:

$ args='a b "c d"'
$ decho $args
a
b
c d

but you'll have to quote the arguments if you pass them on the CL:

$ decho 'a b "c d"'
a
b
c d
like image 2
Dennis Williamson Avatar answered Oct 31 '22 16:10

Dennis Williamson