Bash: converting a string with both spaces and quotes to an array
1
vote
3
answers
3501
views
I have a function (not created by me) that outputs a series of strings inside of quotes:
command
“Foo”
“FooBar”
“Foo Bar”
“FooBar/Foo Bar”
When I try to assign it to an array (Bash; BSD/Mac), instead of 4 elements, I get 7. For example, for
${array}
I *should* get “Foo Bar”
, but instead, I get ”Foo
with the next element being Bar”
. Any element *without* the space works correctly (i.e. ${array}
= “Foo”)
**How can assign each of these elements between the quote including the space to an array that the elements are separated by spaces(?) themselves?**
Right now, I am thinking of using sed/awk to “strip” out the quotes, but I think there should be a better and more efficient way.
Currently, I am assigning the output of the command (looks exactly like the output above including the quotes) to a temporary variable then assigning it to an array.
_tempvar=“$(command )”
declare -a _array=(${_tempvar})
Asked by Allan
(1090 rep)
Jun 15, 2023, 06:46 PM
Last activity: Feb 4, 2024, 10:50 PM
Last activity: Feb 4, 2024, 10:50 PM