Skip to content

Syntax error near unexpected token: 3 Fixes

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 ./NewScript.sh

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

cat NewScript.sh

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.

dos2unix.exe NewScript.sh

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?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. If you're running an Adblocker, we humbly request you to whitelist us.

Share on facebook
Share on whatsapp
Share on twitter
Share on reddit
Share on linkedin
Share on pocket
Share on pinterest
Share on telegram
Share on stumbleupon
Share on digg
Share on tumblr
Share on email
Share on skype
Share on xing
Share on vk
Share on odnoklassniki
Share on mix








>