Alex Rivera | Logout

Copying a Bash array fails

Asked 2012-09-06T15:59:10.793
49

Assigning arrays to variables in Bash script seems rather complicated:

a=("a" "b" "c")
b=$a

echo ${a[0]} 
echo ${a[1]}

echo ${b[0]} 
echo ${b[1]} 

leads to

a 
b 
a
   

instead of

a
b
a
b

Why? How can I fix it?

Edit
Report

1 Answer

79

If you want to copy a variable that holds an array to another name, you do it like this:

a=('a' 'b' 'c')
b=( "${a[@]}" )
answered 2012-09-06T16:01:17.050

Your Answer