I want to learn about arrays and how to assign values to them, so I found this tutorial

While running the following script:

$names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad John")
echo ${names[@]}

I get this error:

line 2: syntax error near unexpected token `[0]="Bob"'
line 2: `$names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad John")'

What am I doing wrong?

  • 3
    Remove $ from names. Not $names but names – Valentin Bajrami Apr 5 '15 at 10:46
  • 3
    Take a look at: shellcheck.net – Cyrus Apr 5 '15 at 10:57
  • Thaks @Cyrus ! That's awesome! – Tanatos Daniel Apr 5 '15 at 10:59
  • [20] ? it does not do what you might expect – Skaperen Apr 5 '15 at 11:12
  • @Skaperen you mean it does not put $USER on the 20th position? – Tanatos Daniel Apr 5 '15 at 11:36

When assigning to a variable, don't use the dollar sign:

names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad John")
  • if you really wanted to use the dollar sign read up on eval – Skaperen Apr 5 '15 at 11:14

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.