在dashshell环境中,我希望将字符串拆分为数组.以下代码适用bash但不适用dash.
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]}
Run Code Online (Sandbox Code Playgroud)
我的问题
是否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
Run Code Online (Sandbox Code Playgroud)