I have:
func() {
echo a "b c"
}
set $(func)
echo 1: $1
echo 2: $2
echo 3: $3
I want to get two arguments: "a" and "b c". How should func() echo to achieve that?
Tried as above. Getting
1: a
2: b
3: c
I want
1: a
2: b c
3:
How should func() echo to achieve that?
It is not possible to do that. No matter the content of func(), it's impossible with set $(func) to achieve that.
Prefer:
func() {
array=(a "b c")
}
func
set -- "${array[@]}"
Do not use, because eval is evil:
func() {
printf "%q " a "b c"
}
eval "set -- $(func)"
local in the function. array is not local and the code works as presented completely fine. In Bash, variables have global (process) scope, unless declared with local or declare in a function.2 mistakes:
You forgot to escape the quotes, so your output is:
$ echo a "b c"
a b c
What you want is:
$ echo a \"b c\"
a "b c"
And you have to evaluate the response:
eval set $(func)
That's all
func() { echo a "b c" } eval set $(func) echo 1: $1 echo 2: $2 echo 3: $3 func() { echo a \"b c\" }
funcisa b c, so how would you know which letters are to be grouped together?