破折号是否支持bash样式数组? [英] Does `dash` support `bash` style arrays?

查看:130
本文介绍了破折号是否支持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样式数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆