Split string into an array in Bash

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

    Use this:

    countries='Paris, France, Europe'
    OIFS="$IFS"
    IFS=', ' array=($countries)
    IFS="$OIFS"
    
    #${array[1]} == Paris
    #${array[2]} == France
    #${array[3]} == Europe
    

提交回复
热议问题