syntax error `(" unexpected in bash script

Viewed 3972

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.

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)
Related