破折号是否支持bash样式数组?

编程入门 行业动态 更新时间:2024-10-19 13:18:28
本文介绍了破折号是否支持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样式数组?

本文发布于:2023-08-02 11:38:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:破折号   数组   样式   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!