In this tutorial, we will learn how to fix the error: syntax error near unexpected token in the Linux system.
This type of error can occur in many cases.
Reason for this error:
This happens when you write some wrong syntax in the command.
For example, you write something in the command but due to a special character in your command that whole string becomes unexpected to the Linux system bash.
Solution of syntax error near unexpected token
This error can occur in multiple incidents so I will cover almost every possible incident and let you know how to fix that.
syntax error near unexpected token `(‘
If you get `(‘ this type of error then the solution is pretty simple:
You just have to wrap the string with single quotes or double quotes. Here is an example:
wget https://www.coderspacket.com:8080/(data.json)
This can throw an error. To fix this we can do this:
wget 'https://www.coderspacket.com:8080/(data.json)'
Or if we wish, we can also use double quotes.
So, the issue is with the bracket.
There is another solution. You can just escape the brackets with escape character \
just before the brackets.
Just like this:
wget https://www.coderspacket.com:8080/\(data.json\)
Someone find another reason for this error.
According to him:
My issue wasn’t related to the un-escaped brackets, but rather because there was already an alias defined with the same name as the function.
In one file, I had an alias like this:
alias foo="echo do something"
And in another file, I had a function with the same name:
foo() {
# Do something else
}
Both of these files were loaded by my ~/.bashrc
file, which resulted in the error message: “syntax error near unexpected token (“.