Shell - Doesn't support array

By xngo on April 7, 2020

I was running my shell script and it gave me the following error message:

Syntax error: "(" unexpected

Solution

It is my fault. I ran a Bash script with sh. sh doesn't support array, that is why it threw a syntax error message. For example, I have the following script.

#!/bin/bash
 
animals=(cat dog fish)
echo ${animals[@]}

If I run it as

sh array-test.sh

I get the error, but if I run it as

./array-test.sh

It is working fine.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.