仅使用内置函数,最简单的方法将CSV字符串拆分为数组并能在Bash和Zsh中运行是什么?我有两个不同的可在Bash和Zsh中运行的代码,但尚未找到适用于两者的方法:
csv='a,b,c'
# Works in zsh, but not in bash
array=(${(s:,:)csv})
# Works in bash, but not in zsh
array=(${csv//,/ }) # This requires that $IFS contains the space character
IFS=, read -a array <<< "$csv"
- 不确定在 zsh 中是否适用。 - Benjamin W.zsh
中,它将是IFS=, read -rA arr <<< "$csv"
。 - anubhava