-2

the below test.sh running in windows command prompt working as expected. however when running inside devcontainer it got failed

files=(
    "first" 
    "second"
    )

for i in "${files[@]}"
do    
    echo $i 
done   

Error running in devcontainer

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

enter image description here

Please guide me how to resolve this error

1
  • 2
    Run it with some shell that supports arrays, e.g. Bash or ksh, not with a standard POSIX shell
    – ilkkachu
    Mar 20 at 13:14

1 Answer 1

6

You are running that using sh which is not bash and does not support arrays. Use bash instead of sh and it should work fine. Alternatively, add a shebang line to the beginning of your script:

#! /bin/bash

Then, make it executable (chmod a+x script.sh) and you can just run it with /path/to/script.sh.

To illustrate the problem:

$ sh -c 'list=("a" "b"); echo "OK"'
dash: 1: Syntax error: "(" unexpected
$ bash -c 'list=("a" "b"); echo "OK"'
OK
1
  • Thanks a lot, yes it is working , i was using in gitbash terminal in windows, however executing with bash test.sh working fine. thanks a lot Mar 21 at 6:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .