Bash Arrays Array from string


Example

stringVar="Apple Orange Banana Mango"
arrayVar=(${stringVar// / })

Each space in the string denotes a new item in the resulting array.

echo ${arrayVar[0]} # will print Apple
echo ${arrayVar[3]} # will print Mango

Similarly, other characters can be used for the delimiter.

stringVar="Apple+Orange+Banana+Mango"
arrayVar=(${stringVar//+/ })
echo ${arrayVar[0]} # will print Apple
echo ${arrayVar[2]} # will print Banana