Bash - 将字符串解析为以逗号分隔的数组

时间:2017-05-18 08:09:28

标签: arrays regex bash delimiter ifs

在Bash中

,如何在Bash中的数组中转换为字符串:

details="cl roo,dara shi,GD leader, is the man,YG FAMILY"

这是我的代码:

IFS="[a-z],[a-z]" read -r -a details_list <<< "$details"

迭代数组时的预期输出:

cl roo
dara shii
GD leader, is the man
YG FAMILY

迭代数组时的实际输出:

cl roo
dara shii
GD leader
is the man
YG FAMILY

1 个答案:

答案 0 :(得分:0)

这应该有效:

details="cl roo;dara shi;GD leader, is the man;YG FAMILY"

IFS=';' read -r -a array <<< "$details"

echo "${array[2]}"   -> GD leader, is the man