say we have:
test() {
echo $1
echo $2
}
and two variables:
a="hello world"
b="$a blabla.."
test $b
it displays:
hello
world
what I want is:
hello world
blabla..
How can I achieve this?
Thanks
say we have:
test() {
echo $1
echo $2
}
and two variables:
a="hello world"
b="$a blabla.."
test $b
it displays:
hello
world
what I want is:
hello world
blabla..
How can I achieve this?
Thanks
Use this:
a="hello world"
b="'$a' blabla"
eval "test $b"
The single-quotes are needed to keep hello world together as a single argument. Then you need to use eval because quotes are not normally processed after variable expansion.
eval if your shell does not support arrays (for instance, you require POSIX compatibility).b, you also have the ability to make b an array instead. I'm not sure what other situation would preclude the use of an array.You want b to be an array, otherwise there's no way with a single string containing whitespace to determine which space char is the separator:
mytest() {
echo "$1"
echo "$2"
}
a="hello world"
b=( "$a" "blah blah" )
mytest "${b[@]}"
hello world
blah blah