How do I split a string on a delimiter in Bash?

后端 未结 30 1921
萌比男神i
萌比男神i 2020-11-21 04:58

I have this string stored in a variable:

IN=\"bla@some.com;john@home.com\"

Now I would like to split the strings by ; delimite

30条回答
  •  灰色年华
    2020-11-21 05:40

    A one-liner to split a string separated by ';' into an array is:

    IN="bla@some.com;john@home.com"
    ADDRS=( $(IFS=";" echo "$IN") )
    echo ${ADDRS[0]}
    echo ${ADDRS[1]}
    

    This only sets IFS in a subshell, so you don't have to worry about saving and restoring its value.

提交回复
热议问题