Reading a delimited string into an array in Bash Reading a delimited string into an array in Bash bash bash

Reading a delimited string into an array in Bash


In order to convert a string into an array, please use

arr=($line)

or

read -a arr <<< $line

It is crucial not to use quotes since this does the trick.


Try this:

arr=(`echo ${line}`);


In: arr=( $line ). The "split" comes associated with "glob".
Wildcards (*,? and []) will be expanded to matching filenames.

The correct solution is only slightly more complex:

IFS=' ' read -a arr <<< "$line"

No globbing problem; the split character is set in $IFS, variables quoted.