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

落爺英雄遲暮 提交于 2020-05-29 10:03:21

问题


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?


回答1:


Specify your interpreter explicitly in the crontab entry. Use

bash /path/zero_check.sh

rather than

/path/zero_check.sh



回答2:


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



回答3:


Just for the documentation, i had an old script to run which had an Syntax Error in the Shebang:

#/bin/bash

instead of

#!/bin/bash

Also check the Script is executable of course.



来源:https://stackoverflow.com/questions/37004196/error-syntax-error-unexpected-when-declaring-arrays-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!