Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 'Syntax error: "(" unexpected' when declaring arrays in bash

Tags:

bash

cron

Same problem as this OP, but must be a seperate cause.

The following script:

#!/bin/sh
arr=("cat" "dog" "bird")

Works interactively (debian) but fails when called by crontab with:

/bin/sh: 2: /path/zero_check.sh: Syntax error: "(" unexpected

I've tried with #!/bin/bash shebang, and declaring array with declare -a arr=("cat" "dog" "bird"), to no effect.

Any idea why?

like image 319
geotheory Avatar asked Oct 17 '25 09:10

geotheory


2 Answers

The problem here is that you are using this shebang:

#!/bin/sh

Whereas arrays are something Bash specific that shell does not allow.

So to make it work, change the shebang of your script to Bash:

#!/bin/bash
like image 155
fedorqui 'SO stop harming' Avatar answered Oct 19 '25 07:10

fedorqui 'SO stop harming'


Specify your interpreter explicitly in the crontab entry. Use

bash /path/zero_check.sh

rather than

/path/zero_check.sh
like image 26
Seth Difley Avatar answered Oct 19 '25 09:10

Seth Difley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!