1

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

While running the following script:

#!/bin/bash
$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?

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

1 Answer 1

3

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .