Convert text string in bash to array

How to convert a string like this in BASH to an array in bash!

I have a str line that contains "title1 title2 title3 title4 title5 title5" (spatial divided names)

I want str to be modified into an array that will store every header in every index.

+7
linux unix bash shell ksh
source share
2 answers

To convert a string to an array, say:

$ str="title1 title2 title3 title4 title5" $ arr=( $str ) 

The shell will split words into spaces if you do not specify a string.

To iterate over the elements in an array created in this way:

 $ for i in "${arr[@]}"; do echo $i; done title1 title2 title3 title4 title5 
+21
source share

Another method using reading:

 read -a array <<< $str 
+1
source share

All Articles