我有一个像这样的字符串:
Spain-South Africa:2-1
我想将其拆分为:
Spain-South Africa
2-1
我尝试将其分割为 IFS=':'
但它给了我:
Spain-South
Africa
2-1
我的代码:
最佳答案
无法重现,但您可能没有为 read
命令正确设置 IFS
,或者没有正确显示输出。
$ str="Spain-South Africa:2-1"
$ IFS=: read -ra results <<< "$str"
$ declare -p results
declare -a results=([0]="Spain-South Africa" [1]="2-1")
根据您短暂的评论,您想要类似的东西
while IFS=: read -ra results; do
...
done < "$1"
而不是
for str in $(cat "$1"); do
...
done
关于linux - 使用 Bash 用 IFS 分割字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59359700/