Table Of Contents
What is it?
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 <script>”. You would notice that the non-printing characters “\r” are not present there any longer.
Third Fix: “echo$SHELL”
When you run as a Shell Script target in any Xcode project and you encounter this error, then you correct this issue by:
- First of all, do not tag “bash” & “sh”; you will have one shell. You can type “echo$SHELL” for knowing which shell you should use or put a shebang during the start of your script.
- Place semicolons after the commands including […] which serves to be an alias for “test”. The Command Terminators tend to be newline. While the terminators like “,”, “;”, “&&”, “II” and & are mandatory. You can place several commands between if & then. Therefore, the semicolons are mandatory.
Fourth Fix “LF” & not “CRLF”
For several users, the error of syntax error near unexpected token occurred due to the incorrect line ending which could be the result of “LF” & not “CRLF”.
This happened because of the users working from the Windows Operating System.
The users can check the same on Notepad++ by:
View > Show Symbol > Show all characters
This could be fixed by following the steps as:
Edit > EOL Conversion > UNIX/OSX Format.
Fifth Fix: Misconfigured System Files
The Syntax Error Near Unexpected Token is an error that is usually encountered by the presence of misconfigured system files.
These common Windows errors are usually easier to fix. If you have received a “syntax error near unexpected token” message, then there maximum chances that your computer system has some registry issues. Therefore, by downloading & running the registry repair tool as “Advanced System Repair”, you can quickly fix this problem effectively.
At the same time, you can also prevent additional problems from occurring.
Once you have fixed the registry, you can perform a quick scan with the help of the PC Health Advisor anti-malware tool that will aid you in ensuring that your computer system has no additional issues.
Here are some steps to follow:
- Download & install the Advanced System Repair tool. Now scan your computer system with this tool.
- Choose the option “Repair All” icon once your system has finished the scanning process,
- Download & Install the “PC Health Advisor Malware Removal” tool.
- The PC Health Advisor Malware Removal tool will help in optimizing your PC by removing all traces of malware including adware, spyware, and so more.
- Select the option “Start New Scan”.
Sixth Fix: General QA
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.
syntax error near unexpected token:
Other Common Issues
- newline’
- fi’
- done’
- then’
- else’
- (‘ python
- $’ r”
- $’do r”
- elif’
- do
- echo’
- (‘ bash
- newline’ doctype html ‘
- (‘ c
- if’
- mac
- (‘ shell
- newline’ mac
- ‘ in shell script
- (‘ linux
- awk
- am_init_automake
- a’
- ‘agg”
- alsa ‘
- (‘ array
- apache ‘
- ampersand
- alias’
- sys.argv’
- newline’ arch ‘
- token (‘ awk
- bash array
- token ampersand
- xcb-aux
- token alias
- os.path.abspath’
- oken newline’
- newline’ line 1 arch ‘
- bash
- bash function
- bash script
- break’
- bashrc
- bracket
- (‘ bash mysql
- brew
- blas
- ‘then’ bash script
- newline’ bash
- else’ bash
- do’ bash
- done’ bash
- fi’ bash
- dist-bzip2′
- $’ r” bash
- elif’ bash
- (‘ git bash
- cd’
- curl’
- configure
- case
- cscope ‘
- check ‘
- cygwin
- cat
- (‘ c program
- config.nice’
- conda.cli.main’
- centos
- config.h’
- (‘ csh
- crypto ‘
- case statement
- cares ‘
- (‘ cpp
- cron
- (‘ comm
- done’ while
- done’ in unix
- do’ while
- do for loop
- done bash script
- disable-static’
- do’ in linux
- done’ cygwin
- deps ‘
- django_settings_module
- do bash
- dbus ‘
- done’ while loop
- done’ while read
- esac’
- elif’ in unix
- export’
- external’
- ‘ exec
- enable’
- e2fs ext2fs’
- else if’
- expr
- ‘ expect
- (‘ eclipse
- elif’ cygwin
- elif in linux
- token echo’
- if else
- from’
- foreign’
- fi’ in shell script
- function
- for loop
- fuse ‘
- find’
- (‘ foreach
- (‘ flask
- file’
- fft ‘
- found_pkgconfig ‘
- freetype ‘
- token from’
- 1.14 flavour
- shell function
- glib ‘
- git-directory’
- grep’
- gtk ‘
- (‘ git
- gpio.board’
- gnu’
- gnutls ‘
- g2 ‘
- (‘ gcc
- ggplot2 ‘
- goto’
- gdk_pixbuf
- gst ‘
- newline’ git
- newline’ golang
- token newline’
- d token (‘
- nexpected token
- hello world ‘
- hello ‘
- (‘ hive
- html
- newline’ html
- d token hello world ‘
- token (‘ in hive
- ted token (‘
- syslog-ng-config.h ‘
- token newline’ doctype html ‘
- ected token
- ected token newline’
- in
- in shell script
- ifeq
- in python
- import
- in unix
- (‘ int main(void)’
- iconv’
- imagemagick’
- illegal
- int’
- token i’
- token if’
- fi shell
- $’in r”
- (‘ in c
- (‘ in mysql
- java
- javascript
- jenkins
- jack ‘
- json
- jquery
- js
- (‘ node js
- os.path.join’
- token (‘
- in json at position 0
- ed token fi’
- token newline’
- d token wall
- unexpected token (‘
- ksh
- kali
- libpng ‘
- libsn ‘
- linux
- libusbmuxd ‘
- libusb
- linux shell script
- line’
- let’
- lzma liblzma ‘
- libcurl ‘
- lt_decl_varnames ‘
- libgnutls ‘
- libxml2 ‘
- ls’
- lasso ‘
- lua
- makefile
- mkdir’
- mac terminal
- mv’
- meaning
- mongodb
- maximum’
- mavx
- (‘ mysql
- (‘ main
- m4′
- matlab
- ^m’
- mongo
- minor-version’
- token (‘ mysql
- fi’ mac
- do mac
Ryan is a computer enthusiast who has a knack for fixing difficult and technical software problems. Whether you’re having issues with Windows, Safari, Chrome or even an HP printer, Ryan helps out by figuring out easy solutions to common error codes.