Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax error `(" unexpected in bash script [duplicate]

i'm coding a simple bash script and i found this error syntax error at line XX `(' unexpected my code:

function myfun(){
   echo XXXX
   echo YYYY
   read choice
}

choice=$(myfun)

where is the error. i used the ShellCheck and no errors were detected.

like image 232
BDeveloper Avatar asked Sep 21 '25 00:09

BDeveloper


1 Answers

Make sure you are running the script with bash. That error is a commonly seen dash shell error.

I suspect the first line of your script is not #!/bin/bash, i.e. you may have left out the shebang line entirely resulting in the default shell being used (which will often be dash especially on Debian derived Linuxes where /bin/sh -> dash).

Try running this:

#!/bin/bash

myfun()
{
   echo XXXX
   echo YYYY
   read choice
}

choice=$(myfun)
like image 57
mattst Avatar answered Sep 22 '25 19:09

mattst