Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy an array in Bash?

Tags:

arrays

bash

copy

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 

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

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

^^ 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.

like image 330
Kyle R. Avatar asked Oct 17 '13 01:10

Kyle R.


People also ask

How do I copy one array to another in bash?

Note: when copying associative arrays, the destination must already exist as an associative array. If not, array_copy() will create it as a standard array and try to interpret the key names from the associative source as arithmetic variable names, with ugly results.

How do you copy an entire array?

If you want to copy the first few elements of an array or a full copy of an array, you can use Arrays. copyOf() method. Arrays. copyOfRange() is used to copy a specified range of an array.

How do I print an array in bash?

Print Bash Array We can use the keyword 'declare' with a '-p' option to print all the elements of a Bash Array with all the indexes and details. The syntax to print the Bash Array can be defined as: declare -p ARRAY_NAME.

How to use an array in Bash?

Bash Beginner Series #4: Using Arrays in Bash 1 Creating your first array in a bash script. Let’s say you want to create a bash script timestamp.sh that updates the timestamp of five different files. 2 Accessing array elements in bash. ... 3 Adding array elements in bash. ... 4 Deleting array elements in bash. ...

How to copy non-associative arrays in Bash?

The simplest way to copy a non-associative array in bash is to: arrayClone=("${oldArray]}")&] or to add elements to a preexistent array: someArray+=("${oldArray[@]}") Newlines/spaces/IFS in the elements will be preserved. For copying associative arrays, Isaac's solutions work great.

How to copy a file from one directory to another through Bash?

The article will show how to copy a file from one directory to another through Bash. You can copy a specific file to a new directory through Bash, followed by the name of the file you want to copy and the directory you want to copy the file into. We have to copy hello.txt in the folder directory, and then we can execute the following command.

How do I reverse an array in Bash?

To reverse an arbitrary array (which may contain any number of elements with any values): With bash 4.4+, given that bash variables can't contain NUL bytes anyway, you can use GNU tac -s '' on the elements printed as NUL delimited records:


2 Answers

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    
like image 65
user1088530 Avatar answered Sep 19 '22 13:09

user1088530


The simplest way to copy a non-associative array in bash is to:

arrayClone=("${oldArray[@]}") 

or to add elements to a preexistent array:

someArray+=("${oldArray[@]}") 

Newlines/spaces/IFS in the elements will be preserved.

For copying associative arrays, Isaac's solutions work great.

like image 20
niieani Avatar answered Sep 20 '22 13:09

niieani