Resolve “syntax error near unexpected token”

Hi, Have you been wondering about the sudden appearance of the “Syntax Error Near Unexpected Token” in your Shell scripts or commands? Then this is the correct place to know more about it. Let’s see why we are seeing the error and how to resolve it.

“Syntax Error Near Unexpected Token” indicates an unexpected element in code. To resolve, check for typos, missing/extra characters, correct syntax, and proper quoting. 

Let’s explore some common scenarios where this error may occur, and provide examples to help you troubleshoot and resolve such issues.

Why we are seeing “Syntax Error Near Unexpected Token”

As mentioned in the introduction, “Syntax Error Near Unexpected Token” is typically encountered when there is an unexpected or misplaced element in your code. It can be for various reasons like 

  • Missing or addition of extra characters in the code or improper use of characters, Incorrect syntax
  • The compatibility issue between Linux and Windows 
  • Copy-Pasting commands

Incorrect syntax

As you know the Shell scripts are widely used in system administration and to automate regular tasks. We can consider a simple example to replicate the issue and understand it better

Example:

mkdir file(1).txt

Running this script might yield the following error:

-bash: syntax error near unexpected token `('

The unexpected token here is the bracket. To fix this error, we need to provide the file name inside the double quotation

mkdir 'test(1).txt'

Similarly, For any command, if you have input with “()” bracket, This will trigger the error again. To resolve it we need to include the input in double quotes as below

Example 1: 

~]# mv test(1).txt test.txt

-bash: syntax error near unexpected token `('

#Include the file name inside the double quotes as below to resolve it

~]# mv "test(1).txt" test.txt

Example 2:

 wget http://www.google.com/files(1).txt

-bash: syntax error near unexpected token `('


#Include the input URL inside the double quote to resolve the issue

~]# wget "http://www.google.com/files(1).txt"

--2023-07-10 14:15:13--  http://www.google.com/files(1).txt

Proxy request sent, awaiting response... 404 Not Found

NOTE: We are getting 404 that is because the URL is incorrect

Let’s discuss a little more complicated problem 🙂

#!/bin/bash

num=(100 200 300 400 500)

for number in ${num[@]} do

  echo $number

done

In this script, we attempt to iterate over an array called numbers using a for loop. However, when executing the script, you may encounter the following error:

./sc.sh: line 4: syntax error near unexpected token `echo'

./sc.sh: line 4: `  echo $number'

The unexpected token, in this case, is the keyword “do”. As you can see the for and do keywords are in the same line.

NOTE: In the shell, If you wanted to provide all the commands in a single line, We need to include “;” after each command

To fix this error, put the do keyword in the next line or add a semicolon “;” before do keyword  as below

#!/bin/bash

num=(100 200 300 400 500)

for number in ${num[@]}

do

  echo $number

Done

Or 

#!/bin/bash

num=(100 200 300 400 500)

for number in ${num[@]}; do

  echo $number

Done

By adding the $ before numbers and using the [@] syntax to expand the array, we can now successfully iterate over the elements of the numbers array and print them without encountering a syntax error.

As I mentioned earlier, Remember to always pay attention to the syntax and structure of your Bash scripts, ensuring that variables, arrays, and other elements are used correctly to avoid “syntax error near unexpected token” issues.

Compatibility issue between Linux and Windows 

Syntax errors can occur when using a text editor like Notepad (Windows/Old version of the Mac). This is because, In Windows/DOS text files a new line is denoted by the combination of a Carriage Return (\r) followed by a Line Feed (\n) character. In UNIX/Linux Line Feed (\n) character to indicate line breaks.

So, Whenever you are trying to edit a script in Windows and executing in Linux, You would most probably hit the “syntax error near unexpected token” error. This is due to the line ending difference between window and Linux

Use dos2unix utility

By using the dos2unix utility, We can convert the above-mentioned difference to its compatible versions

So whenever you are editing a file in Windows, Use the below command in Linux before executing the file

]# dos2unix script.sh 

dos2unix: converting file script.sh to Unix format...

]# ./script.sh

copy-pasting commands

Syntax errors can occur when copy-pasting commands from a text editor like Notepad. This issue is often caused by hidden characters or formatting inconsistencies that are carried over during the copy-paste process.

Use a code editor

We can avoid the above issue by using a code editor instead of a notepad. This is specifically designed for programming or scripting. Code editors like Visual Studio Code, and Sublime Text can help identify hidden characters or formatting issues.

Reformat the code

Let’s say you are using a code from a blog or editing it in a text editor, Make sure to check the inconsistent indentation or line breaks and reformat it to ensure proper formatting and structure before executing the script. This can be done manually or by using code formatting tools available in code editors.

Retype quotes and special characters

While copying and pasting code there are chances the quotes or special characters may not be copied correctly due to formatting issues. You can manually retype the quotes and special characters to ensure correctness.

Conclusion: 

Syntax errors are a common issue that every programmer came across. The “syntax error near unexpected token” is a specific type of syntax error that indicates an unexpected element in the code. We have shared multi examples of how this issue can occur and the steps used to resolve it.

Good Luck with your Learning !!

Read More: Syntax Error Near Unexpected Token Newline

Frequently asked questions (FAQ)

What is syntax error near unexpected token in Linux?

This error message indicates that something unexpected was encountered by the system, resulting in a syntax error. 

Why am I seeing a “syntax error near unexpected token” in my script?

There can be various reasons for this error. It could be due to typo errors, missing or extra characters, incorrect syntax, or misplaced keywords within your code or script.

 How do I fix an unexpected token syntax error?

To fix this error, carefully review your code for any mistakes or inconsistencies. Check for missing or extra characters, ensure proper syntax and formatting, and verify that keywords or special characters are used correctly. 

Jerry Richard
Follow me

Was this post helpful?

Yes
No
Thanks for your feedback!

2 thoughts on “Resolve “syntax error near unexpected token””

Leave a Comment