本文介绍了破折号是否支持bash样式数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在dash shell环境中,我希望将字符串拆分为数组.以下代码在bash中有效,但在dash中无效.
In the dash shell environment I am looking to split a string into arrays. The following code works in bash but not in 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]}我的问题
dash是否支持这种样式的数组.如果没有,那么有什么建议可以将其解析为另一种类型的变量不而不使用循环?
Does dash support arrays in this style. If not are there any suggestions for parsing this out into an another type of variable without the use of a loop?
推荐答案dash不支持数组.您可以尝试这样的事情:
dash does not support arrays. You could try something like this:
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更多推荐
破折号是否支持bash样式数组?
发布评论