如何在Bash中复制数组? [英] How to copy an array in Bash?

查看:112
本文介绍了如何在Bash中复制数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列应用程序,初始化如下:

I have an array of applications, initialized like this:

depends=$(cat ~/Depends.txt)

当我尝试解析列表并将其复制到新数组时,

When I try to parse the list and copy it to a new array using,

for i in "${depends[@]}"; do
   if [ $i #isn't installed ]; then
      newDepends+=("$i")
   fi
done

发生的事情是,只有depend的第一个元素出现在newDepends上.

What happens is that only the first element of depends winds up on newDepends.

for i in "${newDepends[@]}"; do
   echo $i
done

^^这只会输出一件事.所以我试图弄清楚为什么我的for循环只移动第一个元素.整个列表本来是取决于情况的,所以不是全部,但是我全都没有想法.

^^ This would output just one thing. So I'm trying to figure out why my for loop is is only moving the first element. The whole list is originally on depends, so it's not that, but I'm all out of ideas.

推荐答案

a=(foo bar "foo 1" "bar two")  #create an array
b=("${a[@]}")                  #copy the array in another one 

for value in "${b[@]}" ; do    #print the new array 
echo "$value" 
done   

这篇关于如何在Bash中复制数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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