Skip to content

Syntax error near unexpected token: Quick Fix

  • by
  • 3 min read
What is Metadata? Types and benefits | Candid.Technology

Syntax errors in scripts and programs are relatively common, especially for new to the whole coding world. A typo or punctuation omission can waste hours of debugging time. 

In this article, we’re going over the ‘Syntax error near unexpected token’ issue. 

Also read: How to fix ‘wget command not found’ issue in Bash?

What causes Syntax error near unexpected token?

As you can probably guess from the error itself, it’s essentially a syntax error triggered when you try to run a shell script written for an older version of DOS/Windows or Mac systems.

These scripts don’t work even after being syntactically correct because the syntax they’re using is old is now dropped in the modern version of the OS or environment you’re trying to run these scripts. But again, the cause of the error could be a common syntax issue as well.

Also read: How to increment a variable in Bash?

How to fix Syntax error near unexpected token?

Here are few fixes you can go over to resolve the problem.

Check the syntax

The obvious first thing to do is check the syntax of your entire script/program. Make sure you’ve got all the commands right, that there are no typos, no missing punctuation. If you’re coding in a language that’s case sensitive, make sure you’ve got the case right in your commands. 

Referring to the official documentation to cross-check your syntax is never a bad idea as well. Give special attention to things like tab spaces, double spaces, semi-colons, double quotes, escape characters and any mandatory operators required. 

Troubleshoot your shell scripts

If your script runs just fine on your machine and isn’t executing properly, chances are wrong with variables stored during execution. 

In such cases, try running your script with the vx parameter to see what commands are being run and what values are being stored. This helps in figuring out where things are going wrong.

For example, here’s how you’ll run a script with the vx parameter.

sh -vx ./

Once you’ve figured out what’s wrong, you can check the script’s contents using the cat command.


Use dos2unix.exe

If you’re using Cygwin, your scripts will fail to execute because of an extra Carriage Return (\r) character. In such cases, you can use the dos2unix.exe command along with your script to convert it into the correct format.


The above command will fix all formatting issues in your script, and then you can execute it without any issues. 

These should help fix Syntax error near unexpected token.

Also read: How to fix File System Error in Windows 10?


Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: