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

查看:53
本文介绍了如何在 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

结果是,只有depends 的第一个元素最终依赖于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天全站免登陆