3

I am getting an error when using sh, but not bash. Any idea why?

$ sh test.sh
test.sh: 5: test.sh: Syntax error: "(" unexpected (expecting "fi")

#!/bin/bash

if [ 1 -eq 1 ] then declare -a methods=(Method1 Method2 Method3) for i in "${methods[@]}" do echo $i done else echo not found fi

SEU
  • 219

1 Answers1

2

You have a bash hashbang and are running the script with sh. POSIX sh does not support arrays, and while they will still work on some systems there is no guarantee of such, hence the error about the parenthesis.

Use bash test.sh or just make it executable and let the hashbang decide the interpreter.

Also 1 will always equal 1 so your whole if construct is unnecessary.

jesse_b
  • 37,005
  • Thanks. 1 -eq 1 was a place holder to show the issue. This is actually an 'at' job and when I schedule it I get a warning message saying that it will be run using sh. – SEU Sep 03 '21 at 14:44