Does `dash` support `bash` style arrays?

☆樱花仙子☆ 提交于 2019-11-30 17:35:37

问题


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]}

My Question

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?


回答1:


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


来源:https://stackoverflow.com/questions/14594033/does-dash-support-bash-style-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!