Split string into an array in Bash

后端 未结 22 2691
故里飘歌
故里飘歌 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 04:50

    This works for me on OSX:

    string="1 2 3 4 5"
    declare -a array=($string)
    

    If your string has different delimiter, just 1st replace those with space:

    string="1,2,3,4,5"
    delimiter=","
    declare -a array=($(echo $string | tr "$delimiter" " "))
    

    Simple :-)

提交回复
热议问题