Split string into an array in Bash

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

    Another approach can be:

    str="a, b, c, d"  # assuming there is a space after ',' as in Q
    arr=(${str//,/})  # delete all occurrences of ','
    

    After this 'arr' is an array with four strings. This doesn't require dealing IFS or read or any other special stuff hence much simpler and direct.

提交回复
热议问题