Split string into an array in Bash

后端 未结 22 2819
故里飘歌
故里飘歌 2020-11-22 04:24

In a Bash script I would like to split a line into pieces and store them in an array.

The line:

Paris, France, Europe

I would like

22条回答
  •  天命终不由人
    2020-11-22 05:07

    if you use macOS and can't use readarray, you can simply do this-

    MY_STRING="string1 string2 string3"
    array=($MY_STRING)
    

    To iterate over the elements:

    for element in "${array[@]}"
    do
        echo $element
    done
    

提交回复
热议问题