Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

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

1 Answer 1

up vote 3 down vote accepted

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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