1 问题
我们有段字符串data="chenyu:chenzixuan:chenzi",我们需要通过:分割,我们可以采用IFS
我们可以采用下面的模板
oldIFS=$IFS IFS=":" for item in $data; do ******* done IFS=$oldIFS
2 代码实现
#/bin/bash data="chenyu, chencaifeng, chenzixuan, chenzixi" oldIFS=$IFS IFS=", " for item in $data; do echo Item:$item done IFS=$oldIFS line="chen:gong:yu:chen:cai:feng"; oldIFS=$IFS IFS=":" count=0 for item in $line; do [ $count -eq 0 ] && user=$item [ $count -eq 4 ] && name=$item # count=$[$count + 1]; let count++ done IFS=$oldIFS echo $user echo $name
3 运行结果
Item:chenyu Item:chencaifeng Item:chenzixuan Item:chenzixi chen cai