How to read a space-delimited string into an array in Bash?
arrays, bash, delimiter, shell, string
Solution
In order to convert a string into an array, create an array from the string, letting the string get split naturally according to the `IFS` (Internal Field Separator) variable, which is the space char by default:
arr=($line)
or pass the string to the stdin of the `read` command using the herestring (`<<<`) operator:
read -a arr <<< "$line"
For the first example, it is crucial not to use quotes around `$line` since that is what allows the string to get split into multiple elements.
See also: https://github.com/koalaman/shellcheck/wiki/SC2206
Problem
I have a variable which contains a space-delimited string: ``` line="1 1.50 string" ``` I want to split that string with space as a delimiter and store the result in an array, so that the following: ``` echo ${arr[0]} echo ${arr[1]} echo ${arr[2]} ``` outputs ``` 1 1.50 string ``` Somewhere I found a solution which doesn't work: ``` arr=$(echo ${line}) ``` If I run the echo statements above after this, I get: ``` 1 1.50 string [empty line] [empty line] ``` I also tried ``` IFS=" " arr=$(echo ${line}) ``` with the same result. Can someone help, please?