How to Fix “bash syntax error near unexpected token” in Linux

Bash (Bourne Again Shell) is a command-line program that accepts commands provided and executes them. It takes Linux commands directly typed into it interactively from a keyboard or from a shell script file.

Bash is used in Linux and Mac systems to run the system and it is the default shell in a majority of modern Linux distributions such as Ubuntu, Debian, Fedora, Rocky Linux, and AlmaLinux to mention a few.

One of the most common errors that users encounter when executing shell scripts or commands on the bash shell is “bash: syntax error near unexpected token ‘(‘“. This might appear somewhat confusing and you might wonder where you went wrong with executing the command.

-bash syntax error near unexpected token ‘(‘ Error

The error is mainly attributed to an operation carried out on a file whose file name is enclosed by parentheses or round brackets.

Whenever you use parentheses with file names or in shell scripts, you MUST ALWAYS ESCAPE them. Failure to do so, you are likely to run into this type of error.

In bash, parentheses or round brackets are considered special characters, and therefore, must be escaped when performing file operations such as creating, copying, removing, and downloading a file.

Let’s take a simple example and simulate the error. When you create a file, say, sample_file(data).txt using the touch command, you will run into the following error message as shown.

$ touch sample_file(data).txt

bash: syntax error near unexpected token `('
bash: syntax error near unexpected token Error
bash: syntax error near unexpected token Error

Now let us switch gears and check out ways in which you can resolve the above error.

Fix 1: Use a Backslash to Escape Parenthesis

If you insist on having a file name with parenthesis, the solution is to prefix each of the parentheses brackets with a backslash. This is popularly known as escaping the parenthesis and takes the following format: \( and\). The backslash character comes right before each parenthesis.

In our earlier example, we would create our file as follows without any problems.

$ touch sample_file\(data\).txt

From the following output, you can see that we have successfully created the file without any issues by escaping the parentheses.

Use Backslash to Escape Parenthesis
Use Backslash to Escape Parenthesis

The same principle applies when copying, removing, and renaming the file as shown below.

$ cp sample_file\(data\).txt /tmp/
$ rm sample_file\(data\).txt

Fix 2: Enclose File Inside double Quotes

The other alternative is to enclose the entire file name inside double quotation marks. This trick will also work without an issue.

$ cp "sample_file(data).txt" /tmp
$ rm "sample_file(data).txt"
Enclose File Inside Double Quotes
Enclose File Inside Double Quotes

Using the above methods will help you come out of this error and seamlessly perform operations on your files.

Conclusion

And there you go! In this brief guide, we have shown you how to fix the “bash: syntax error near unexpected token ‘(‘” error in Linux. As you’ve seen, the fix is quite simple and straightforward. Your feedback is welcome.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Winnie Ondara
My name is Winnie, a Linux enthusiast and passionate tech writer in Linux and DevOPs topics. I enjoy keeping abreast with the latest technologies in the Linux ecosystem and trying out new tools provided by the FOSS community.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 Comment

Leave a Reply
  1. Just a quick note: Single Quotes will also work, e.g., 'sample_file(data).txt'.

    In fact, you needn’t enclose the entire filename in (single or double) quotes; as long as the “special” characters are within the quotes, you will be fine, e.g., all of the following will also work:

    sample_file'('data')'.txt
    sample_file"("data")".txt
    sample_file'('data")".txt
    sampl'e_file('d'ata)'.txt
    sample_"file(data)".txt
    

    and so on, and so on, …

    Far for me to argue that they are all easily readable, but they do work.

    You could, in this way, even address a filename (or, for that matter, any arbitrary string) that has both single and double quotes in it; just enclose the single quotes in double quotes and vice versa, and you will be fine.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.