How Do You Troubleshoot the Shell/Bash Script Error “syntax error near unexpected token”?

Problem scenario
You are trying to run a bash/shell script but you get an error message that is difficult to troubleshoot.  It looks something like this:

myscript.sh: line 4: syntax error near unexpected token `curl'
myscript.sh: line 4: `curl 'http://10.10.0.1' '

You thoroughly analyzed the line of the script that is called out in the error.  What is the root cause?

Solution
Did you define a function in the script?  The true problem may not be on the line that is called out in the error message.  If you defined a function like this "coolfunction{", this will cause the above problem.  The error message is misleading.  You need a space after the text "coolfunction" but before the opening brace "{".

Leave a comment

Your email address will not be published. Required fields are marked *