The following snippet behaves exactly as I want it to:
array=( "one" "" "three" ); echo "${array[@]}"; two=${array[1]}; echo two=$two
It returns:
one three
two=
So the variable $two is assigned to be empty. Perfect. Now let's try to capture some output from another script, here, we'll say the "script" is a simple echo.
array=( $( echo "one" "" "three" ) ); echo "${array[@]}"; two=${array[1]}; echo two=$two
This returns:
one three
two=three
Uh oh. But this one is simple, echo is killing the quotes as it processes its arguments. Let's protect those:
array=( $( echo \"one\" \"\" \"three\" ) ); echo "${array[@]}"; two=${array[1]}; echo two=$two
Now we get:
"one" "" "three"
two=""
So instead of getting an empty variable, all the variables are wrapped in quotes even after the echo. These quotes are very annoying to remove (you can use eval or sed or something but speed is critical for this application).
So why does:
array=( "one" "" "three" ); echo "${array[@]}"; two=${array[1]}; echo two=$two
behave differently from: array=( $( echo \"one\" \"\" \"three\" ) ); echo "${array[@]}"; two=${array[1]}; echo two=$two
even though:
$( echo \"one\" \"\" \"three\" )
returns:
"one" "" "three"
Or, more generally, what is the appropriate output of a script such that it can be passed into an array? (Or should I just create a function within the "outer" script that calls the "inner" script as a function where it can easily get the array returns).
Thanks