+-
linux – “dash”支持`bash`样式数组吗?
在dash shell环境中,我希望将字符串拆分为数组.以下代码适用于bash但不适用于破折号.

IFS=""
var="this is a test|second test|the quick brown fox jumped over the lazy dog"
IFS="|"
test=( $var )
echo ${test[0]}
echo ${test[1]}
echo ${test[2]}

我的问题

dash是否支持此样式的数组.如果没有,是否有任何建议将此解析为另一种类型的变量而不使用循环?

最佳答案
dash不支持数组.你可以尝试这样的事情:

var="this is a test|second test|the quick brown fox jumped over the lazy dog"
oldIFS=$IFS
IFS="|"
set -- $var
echo "$1"
echo "$2"
echo "$3"      # Note: if more than $9 you need curly braces e.g. "${10}"
IFS=$oldIFS
点击查看更多相关文章

转载注明原文:linux – “dash”支持`bash`样式数组吗? - 乐贴网