4

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?

  • On Zsh I get nothing output. As this is a bash question, it is maybe worth noting for ZSH users who came by accidentally. – Timo Nov 26 at 7:50
8

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

| improve this answer | |
  • 2
    The quotes aren't necessary, just the whitespace (not a comma) to create two items instead of one. – chepner Sep 25 '15 at 13:42
1

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 '{ }'
| improve this answer | |
-1

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

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
| improve this answer | |
  • Welcome to the site, and thank you for your contribution. Unfortunately it is not quite clear what your answer refers to. The solution to the OPs question was indeed that the array was incorrectly initialized with a comma-separated list instead of a space-separated one. Also, neither the question nor any of the answers uses a $-style dereferencing of the array index. Please edit your answer to clarify its scope. – AdminBee Apr 8 at 14:14
  • @AdminBee The question asked is "How do I access an item of an array in shell?" which I answered. – Roland Apr 8 at 14:20
  • @AdminBee In spite of no one using $ indexing I still thought it important to mention. – Roland Apr 8 at 14:21
  • 1
    No, the issue is that the user gets 1,2 when expecting 1. You don't even mention why this is or what to do about it. Accessing the first element of the array is something the user is already doing correctly. – Kusalananda Apr 8 at 14:37
  • 1
    @Roland I made the title relevant to the question. I never edited the text of the question, so the question remains the same. – Kusalananda Apr 9 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.