Bash – Unexpected output when accessing array element

arraybashshell

I am using terminal with MacOSX.

I read this entry introducing about unix array. I tried to access an array as its way, but failed:

a=(1,2) && echo "${a[0]}"

this is the output:

1,2

What I expected is 1.

What should I do?

Best Answer

a=('1' '2') && echo "${a[0]}" would be better like this.

In your version you only created a one element array which contains the value "1,2".

Related Question