加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – “dash”支持`bash`样式数组吗?

发布时间:2020-12-14 00:04:43 所属栏目:Linux 来源:网络整理
导读:在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]} 我的问题 d
在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读