This problem (syntax error near unexpected token) usually occurs in Cygwin, a Unix-type of environment and command-line interface for Microsoft Windows.
It will be triggered when you try to run a shell script which has been created or edited in the older Mac systems or Windows/DOS.
In Windows/DOS text files, a new line is usually the combination of two characters including a Carriage Return (r) which is followed by a Line Feed (n) character.
Prior to Mac OS X in Mac systems, a line break used to be the single Carriage Return (r) character. Modern Mac OS & Linux or Unix systems make use of the Unix style Line Feed (n) line breaks.
Cygwin might fail to process the available scripts that are formatted in Old Mac OS or Windows/DOS systems because of the presence of the extra Carriage Return (r) character.
How to Fix The “Syntax Error Near Unexpected Token” Error?
First Fix: Use The “dos2unix.exe” Command
To solve the problem of the error “syntax error near unexpected token”, the users are recommended to make use of the “dos2unix.exe” command.
This command will be useful in converting the script to the Unix readable format.
Second Fix: Use The “cat” Command
If you are making use of a Linux system, then you can follow the steps:
Make use of the “cat” command for displaying the contents of the script.
Then, you can utilize the commands of copy & paste for copying what was displayed. Paste the copied content on a new file. The new file would visually appear to be similar as the old one. However, it will no longer contain the non-printable characters and will remove the errors.
Set the permissions on the file that has been newly created such that it can be executed.
Run the new script by using the “sh –vx
Correct the error “syntax error near unexpected token” by following these steps!
Unix VS Linux
You’ve probably heard of the word Linux. You probably know it’s an operating system.
In fact, it’s the kernel that’s employed at the core of the Android operating system. It also operates on an array of devices, including desktops and laptops. It really is the hub of Chrome OS. You can get it on servers, it also runs on Raspberry Pi.
Now you might have also heard of Unix and of course, they appear similar. And you might be wondering: are they exactly the same thing? Could they be compatible?
Are they totally different? If you would like to know what is the difference between UNIX and Linux, this coding vlogger explains: to comprehend the difference between these two operating systems, you need to know a bit about the history of how we got to where we are today
UNIX was invented by two men, that is Ken Thompson and Dennis Ritchie. Now you may have heard those two names before. Dennis Ritchie, needless to say, is renowned for creating the C programming language.
Ken Thompson is very renowned for not only inventing lit UNIX, but he also created the utf-8 character encoding that people make use of all the time today and he is the co-inventor of Google’s Go programming language.
So we’re dealing with legends in computing.
In the late 1960s early 1970s, Ritchie and Thompson, along with people like Kernighan, were implementing an operating system called Multics (Multiplexed Information and Computing Service). That was built to run multiple programs. Watch the video for a full history!
Additional Resources
If you have some specific issues with the following- click the link to visit the relevant support forum.
Comments are closed