Split string into an array in Bash

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

    UPDATE: Don't do this, due to problems with eval.

    With slightly less ceremony:

    IFS=', ' eval 'array=($string)'
    

    e.g.

    string="foo, bar,baz"
    IFS=', ' eval 'array=($string)'
    echo ${array[1]} # -> bar
    

提交回复
热议问题