I am using terminal with MacOSX. I am new to shell and I need to do something with array.

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?

up vote 4 down vote accepted

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

  • 1
    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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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