1

When I try to execute the following

#!/bin/sh

folders=("/usr/include")
for i in ${folders[@]}; do
        echo ${i}
done
exit 0

I get test.sh: 3: test.sh: Syntax error: "(" unexpected. I also tried using #!/bin/bash but it didn't work neither. I tried this on Debian Jessie in a script and it produced the above mentioned error. The same script runs fine on Ubuntu 14.04.

Additionally I tried

# foo=(/usr/include); echo $foo
/usr/include

which worked well. I also checked it for non-printable characters with cat -e test.sh

#!/bin/sh$
$
folders=("/usr/include")$
for i in ${folders[@]}; do$
    echo ${i}$
done$
exit 0$

Edit

I executed the script as sh ./test.sh

mistapink
  • 1,926
  • 1
  • 26
  • 37
  • `/bin/sh` is not Bash and has no arrays; you have to use `/bin/bash`, which, as I see just now, you say you did... hmmmm. – Benjamin W. Mar 07 '17 at 20:42
  • How do you call the script? – Benjamin W. Mar 07 '17 at 20:43
  • 1
    I used ```sh ./test.sh```. I just used ```chmod +x test.sh && ./test.sh``` and it seems to work with ```#!/bin/bash```. – mistapink Mar 07 '17 at 20:47
  • Yep, `sh ./test.sh` will run it with `dash`, which is more strictly POSIX than bash. See [this](https://wiki.ubuntu.com/DashAsBinSh). Separately, [this question](https://stackoverflow.com/questions/6499486/how-to-mark-an-array-in-posix-sh) has some thoughts about arrays in POSIX shells. – cxw Mar 07 '17 at 20:48
  • Yes - if you call it `sh ./test.sh`, it doesn't matter what you have in the hashbang line. It would also work with `bash ./test.sh`, no matter what's in the hashbang line. – Benjamin W. Mar 07 '17 at 20:49
  • BenjaminW. cxw is one of you going to post it as an answer. I added how I called the script initially to the question. – mistapink Mar 07 '17 at 20:59
  • I'm actually fairly sure it's a duplicate, let me check. – Benjamin W. Mar 07 '17 at 21:49
  • [Bash array: Unexpected Syntax error](http://stackoverflow.com/questions/11026192/bash-array-unexpected-syntax-error) has the exact same problem, but no good specific answer (and it's closed as "too localized"). – Benjamin W. Mar 07 '17 at 21:51

1 Answers1

0

With bash, you should use declare -a :

#!/bin/bash

declare -a folders=("/usr/include");
for i in ${folders[@]}; do
  echo ${i}
done
exit 0
SegFault
  • 1,097
  • 1
  • 14
  • 14
  • While this answer might not be wrong, the problem was solved using the proper calling convention ```bash ./test.sh```. I guess the problem would remain the same if I called your script with ```sh ./test.sh```. – mistapink Mar 07 '17 at 21:01
  • Sure, you're right, this syntax is not valid for `sh`, but the OP question was for `bash`. And yes, sorry, I didn't noticed the problem was already solved. – SegFault Mar 07 '17 at 21:03
  • Ok, I understand now, it was called with `sh`... – SegFault Mar 07 '17 at 21:05
  • You can use `declare -a` to declare an array, but it is not necessary. Using the array assignment syntax `a=(...)` automatically sets the array attribute on a name. – chepner Mar 07 '17 at 21:44