在bash中,是有可能把多个数组内的报价,然后访问它们? [英] In bash, is it possible to put several arrays inside quotes and then access them?

查看:128
本文介绍了在bash中,是有可能把多个数组内的报价,然后访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我能做到这一点:

I know I can do this:

set=("1 2 3" "4 5 6")
for subset in "${set[@]}"
do
for element in $subset
do
echo $element
done
done

1 2 3 4 5 6将顺序打印。不过,我可以的不可以做到这一点:

  set="(1 2 3) (4 5 6)"
  for subset in $set
  do
  echo ${subset[2]}
  done

我要打印3 6.为什么我想这样做的原因是,我想有机会获得任何元素我要迭代的迭代,而不是逐个期间访问。这就是为什么我试图把阵列内的报价,而不是把报价一大阵里面。有没有办法做到这一点?
谢谢,

I want to print 3 6. The reason why I want to do this is that I want to have access to whichever element I want to access during iteration instead of iterating one by one. That's why I try to put arrays inside quotes instead of putting quotes inside a big array. Is there any way to do this? Thanks,

推荐答案

不幸的是,我不认为bash的支持多维数组,这听起来像你在找什么。您可以从庆典本身的一点帮助,像这样进行模拟:

Unfortunately, I don't think bash supports multi-dimentional arrays, which sounds like what you're looking for. You can simulate it with a little help from bash itself like so:

x=()
x+=("1,2,3")
x+=("4,5,6")

for val in ${x[@]}; do
    subset=($(echo $val | tr ',' ' '))
    echo ${subset[2]}
done

这篇关于在bash中,是有可能把多个数组内的报价,然后访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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