BASH Programming

How to Resolve Bash Terminal Error: “Bash: Syntax Error Near Unexpected Token ‘Newline’

When working with the Bash script, you can encounter the unexpected token “newline” error which affects the execution of your script. Understanding this error is the first step to solving it. This post discusses everything about this error. We will understand what triggers it and how you should resolve it to avoid reencountering it. Let’s begin!

What Is the Bash: Syntax Error Near Unexpected Token “Newline”

When executing a Bash script, you may run into the unexpected token “newline” error due to different reasons. This error occurs when Bash detects unexpected characters in your script or input.

If you are facing this error, a syntax error in your script is causing it. We will give different instances where the error can occur and see how to resolve it to fix the error. Take a look at the following examples.

Example 1: Unquoted Angle Brackets

Quote any angle brackets that you use within your code when writing a Bash script. Only then will Bash read it as an angle bracket. Otherwise, it raises the unexpected token “newline” error.

In the following image, we tried to execute a Bash script on the terminal and got the syntax error as follows:

The first thing to do when encountering such an error is to open your script using a text editor. We use the nano editor for this case.

When you open the script, look for any code which contains unquoted angle brackets. That’s the main cause of the error. In the previous image, we can see that our str variable has an unquoted string, and that’s the cause of the syntax error. You can fix this error by quoting it as shown in the following:

If we quote it as expected by Bash and rerun the same script, we will manage to run the script successfully.

Example 2: Using the Angle Brackets in the Bash Command Line Parameters

When you have a Bash script that takes an input from the user to use as arguments in a parameter in the script, adding the angle brackets without quoting them raises the unexpected token “newline” error.

If you want to include an argument that contains the angle brackets, make sure that you quote it. If we open the Bash script, we can check the syntax to identify the origin of the error.

In the previous script code, we can see that we are accepting the arguments from the user input. Thus, when we run the script, we must add the arguments as we did, causing the syntax error to appear. In the user input, we notice that we entered an angle bracket in the second argument which causes Bash to raise the syntax error.

However, if we quote the angle bracket and enter it again, we successfully run the Bash script without errors.

In the output, after quoting the angle brackets, we managed to print them on the terminal as per the script.

So, if you get the “Bash: syntax error near unexpected token “newline’” error, start by checking the code in your script to determine where the error is. Moreover, check if you have parameters in your code and if the error occurs due to an error in the argument that you enter on your terminal.

Conclusion

You can encounter the “Bash: syntax error unexpected token “newline”” when working with the Bash script due to two main reasons: you may not have quoted the angle brackets. Still, you could have an error in your argument. This post detailed these two causes and how to fix the error.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.