1

For an script I'm making I need to convert the output of a command to an array. For simplifying I have made an example using echo:

arr=( $(echo '"test example" "test2 example"') )

What I want is the first element of the array to be

test example

but when doing this:

echo ${arr[0]}

I get

"test

What I have to do to get the result I want?

4
  • 2
    You don't need the echo there. What you can do is arr=("test example" "test2 example"); echo "${arr[0]}". This is how you create an simple array in bash. Commented Apr 1, 2017 at 9:53
  • @val0x00ff "For an script I'm making I need to convert the output of a command to an array.For simplifying I have made an example using echo" The example here is made with echo because people can test it. The real script executes another program. So I can't simply remove echo.
    – mmarquezs
    Commented Apr 1, 2017 at 10:17
  • What does the output of the actual command look like? The format you show here, with quotes, is not the best way to do it: it's hard to parse in a way that makes sense (the eval method suggested in an answer is dangerous since the output of the command will be evaluated — whatever controls the output of that command can cause your script to run whatever program they want) and the definition is ambiguous (e.g. can you have a " in an element? how? What do you do if the output doesn't contain balanced quotes? …). Commented Apr 1, 2017 at 14:19
  • The output is : "word word" "word word" (...) It outputs two words with a space in the middle of them surrounded by quotes and each one of these separated by spaces. If there are quotes they are escaped \". The eval as you and @ilkkachu said has risks. I might look into either changing the program I execute if I can or making my own replacement so the output can be more easily used, maybe using mapfile.
    – mmarquezs
    Commented Apr 3, 2017 at 8:18

2 Answers 2

1

Suppose echo do not produce right output like a command so have to include sed

mapfile -t arr < <(
    echo '"test example1" "test2 example2"' |
    sed 's/" "/"\n"/g'
)
2
  • I like this approach better, using mapfile, but it doesn't seem to work. The array appears empty. :S
    – mmarquezs
    Commented Apr 3, 2017 at 8:23
  • @MarcMarquezSantamaria You are right. All time forgot subprocesses. Settled.
    – Costas
    Commented Apr 3, 2017 at 11:14
1
eval "arr=( $(echo '"test example" "test2 example"') )"

echo "${arr[0]}"

for e in "${arr[@]}"; do
   echo "<$e>"
done

output

test example

<test example>
<test2 example>
1
  • 1
    @MarcMarquezSantamaria, eval is the easy way here, but just note that it runs the whole input as a shell command, doing much more than interpreting the quotes. Variables in the quoted strings will be expanded, so if the output contains $ it may get corrupted. Also, anything outside quotes will be taken as shell syntax, e.g. an unquoted ) will break the array assignment syntax. Also, if someone actively wants to ruin your day, command expansions will be an easy way to do that.
    – ilkkachu
    Commented Apr 1, 2017 at 11:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .