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.

Similar Posts