0

I have a variable:

var='aaa bbb "ccc ddd"'

And the following argument-printing ./script.sh:

#!/bin/bash

for arg in "$@"; do
    echo "$arg"
done

Expected outcome:

$ ./script.sh $var
aaa
bbb
ccc ddd

Actual outcome:

$ ./script.sh $var
aaa
bbb
"ccc
ddd"
$ ./script.sh "$var"
aaa bbb "ccc ddd"
$ ./script.sh ${var@Q}
'aaa
bbb
"ccc
ddd"'

Is there a way to make it happen?

I don't have control over $var - it can't be an array, for example.

koorkevani
  • 321
  • 1
  • 8
  • How would the content of `var` look like, if it were an array? You need to give an example at least, similar as you provided it for the scalar. Also it is not clear what the expected outcome should be for `var="aaa bbb 'ccc ddd'"` or `var='aaa bbb \`ccc ddd\`'`. You would need to define, what quotes are allowed inside of `var`. – user1934428 Dec 27 '21 at 08:33
  • As an array it's simple: `arr=(aaa bbb "ccc ddd")`, `./script.sh "${arr[@]}"`. My point was, it can not be an array, I get `$var` as a given and need to work with it – koorkevani Dec 27 '21 at 08:37
  • Sorry,misread your question in this point. Still, you need to clarify the other issues I mentioned. Also, it would be important to know what other characters can appear inside `var`. For instance, are there only letters and spaces and quotes? – user1934428 Dec 27 '21 at 08:40
  • `var` is what it is. I have no control over it. I want to pass it to `./script.sh`, using some bash-quoting-voodoo-magic, in a way that will give me the expected output. I want to to work as if I did `./script aaa bbb "ccc ddd"`, but using `var` instead of the explicit arguments. – koorkevani Dec 27 '21 at 08:47
  • 1
    Here are some previous questions may be relevant: ["Reading quoted/escaped arguments correctly from a string"](https://stackoverflow.com/questions/26067249/reading-quoted-escaped-arguments-correctly-from-a-string) and ["Get bash to respect quotes when word splitting subshell output"](https://superuser.com/questions/1529226/get-bash-to-respect-quotes-when-word-splitting-subshell-output). – Gordon Davisson Dec 27 '21 at 08:57
  • @koorkevani : You need to know at least, what the content of the variable can be, i.e. need somehow have a grammar for it. Otherwise you can't write a parser, or decide, which existing parser (`eval`, `xargs`, ...) would do what you want. – user1934428 Dec 27 '21 at 09:00
  • @user1934428 my example was pretty much as complex as it gets. I believe `xargs` will do – koorkevani Dec 27 '21 at 09:07
  • The problem is how bash interprets your input. Once you use unquoted $var, it will be spit into 4 values: aaa bbb "ccc ddd" The quotation Marks will not get interpreted. To force it to do so, you would need to do a eval ./script.sh $var But this is dangerous and should never be used with variables you don't know! You would need to interpret the parameters handed over to your script and interpret quotation marks to do it the correct way ... – Ralf Draeger Dec 27 '21 at 11:00

1 Answers1

0

Found a solution!

$ echo "$var" | xargs ./script
aaa
bbb
ccc ddd
koorkevani
  • 321
  • 1
  • 8
  • Just out of curiosity: If you set `var='aaa bbb ccc ddd"'` (i.e. the first double quote omitted), do you still get the result you expect? – user1934428 Dec 27 '21 at 09:05
  • xargs exits with code 1, which I guess is what I expect from a parsing error. It does run `./script.sh` with all the well-formatted arguments, which is not ideal, but in my case `$var` is trusted, so either way it's not a problem – koorkevani Dec 27 '21 at 12:25