Split string into an array in Bash

后端 未结 22 2582
故里飘歌
故里飘歌 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:03

    This is similar to the approach by Jmoney38, but using sed:

    string="1,2,3,4"
    array=(`echo $string | sed 's/,/\n/g'`)
    echo ${array[0]}
    

    Prints 1

提交回复
热议问题