Split string into an array in Bash

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

    Another way to do it without modifying IFS:

    read -r -a myarray <<< "${string//, /$IFS}"
    

    Rather than changing IFS to match our desired delimiter, we can replace all occurrences of our desired delimiter ", " with contents of $IFS via "${string//, /$IFS}".

    Maybe this will be slow for very large strings though?

    This is based on Dennis Williamson's answer.

提交回复
热议问题