Alex Rivera | Logout

Bash array: Unexpected Syntax error

Asked 2012-06-14T03:17:54.460
20

I am writing this simple bash script as follows.

#!/bin/bash

array=( /home/abc/Downloads/something.bat /home/abc/Downloads/smb.conf )
echo ${array[@]}

I expected it to print all the names of the files in the array. But I get this error instead:

test.sh: 3: Syntax error: "(" unexpected

If I change the declaration of array to

array = {/home/abc/Downloads/something.bat /home/abc/Downloads/smb.conf}

this error goes away but I still have new errors

test.sh: 3: array: not found
test.sh: 4: Bad substitution

How can I resolve this issue? This is my first time in shell programming so I am unable to fix the issues myself.

RESOLVED:

I was executing it as sh test.sh but I forgot I had to execute it as bash test.sh

Edit
Report

1 Answer

3

Pointer: Spaces are important with variable assignment in BASH. Don't use them before or after the equal sign.

answered 2012-06-14T03:30:36.553

Your Answer