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

后端 未结 30 2653
萌比男神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:37

    Here is a clean 3-liner:

    in="foo@bar;bizz@buzz;fizz@buzz;buzz@woof"
    IFS=';' list=($in)
    for item in "${list[@]}"; do echo $item; done
    

    where IFS delimit words based on the separator and () is used to create an array. Then [@] is used to return each item as a separate word.

    If you've any code after that, you also need to restore $IFS, e.g. unset IFS.

提交回复
热议问题