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
Variable assignments can't have a space around the =
sign:
array=( /a/b/ /c/d )
^--no spaces
are you sure?
marc@panic:~$ array =(a b)
bash: syntax error near unexpected token `('
marc@panic:~$ array= (a b)
bash: syntax error near unexpected token `('
marc@panic:~$ array = (a b)
bash: syntax error near unexpected token `('
marc@panic:~$ array=(a b)
marc@panic:~$ echo ${array[1]}
b
Pointer: Spaces are important with variable assignment in BASH. Don't use them before or after the equal sign.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With