Question: I am using terminal with MacOSX. 

I read this [entry](http://www.tutorialspoint.com/unix/unix-using-arrays.htm) 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?

Answer: 
Three things to remember while accessing an element in an index array:

1. Normal expression - hardcoded index
======================================

```
arr=(one two three)
element_0="${arr[0]}"

```

2. Normal expression - index stored in a variable
=================================================

```
arr=(it is cool to write readable code)
for ((i=0; i < ${#arr[@]}; i++)); do
    current_element=${arr[i]} # note, no '$' necessary before i
done

```

3. Arithmetic expression involving array elements
=================================================

```
arr=(100 150 350)
((sum_of_first_two = arr[0] + arr[1])) # no need for '{ }'

```
Although your *actual* problem was different from what the question title indicates (your approach to accessing the array item was correct), here are some general remarks on how to work with bash arrays.

From the [Bash Reference Manual](https://www.gnu.org/software/bash/manual/html_node/Arrays.html)

> 
> Any element of an array may be referenced using `${name[subscript]}`.
>  The braces are required to avoid conflicts with the shell’s filename
>  expansion operators.
> 
> 
> 

...

> 
> The subscript is treated as an arithmetic expression that must
>  evaluate to a number.
> 
> 
> 

This implies that no `$` is necessary in the subscript.

> 
> If the subscript used to reference an element of an indexed array
>  evaluates to a number less than zero, it is interpreted as relative to
>  one greater than the maximum index of the array, so negative indices
>  count back from the end of the array, and an index of -1 refers to the
>  last element.
> 
> 
> 

E.g.:

```
declare -i i=1 # -i declares integer
${arr[i*2]} 
${arr[-1]} #returns last element
${arr[0]}  #first element is at index 0

```