syntax error: unexpected end of file

The syntax error at the unexpected end of the file line is caused due to mismatched structure of the code. The machine executes the code line by line, so the syntax needs to be correct, with proper use of quotes (“”), brackets (), and other formatting. Several code editors provide code formatting, like showing interactive colors, which can be helpful for the users. Vim shows different colors and code formatting, which removes the chances of bracket missing.

This code will discuss the following topic in this guide:

Let’s start!

What is a Syntax Error an Unexpected End of File Error in Linux?

As the name indicated, this error is caused due to syntax error, which is a user’s side mistake. There are several types of syntax errors, for instance, the bracket is not closed, and others. When there is a syntax error in the code, the output displays the below error:

Reason: Incorrect Syntax

The below code is written to execute an if condition, which shows “Target Achieved” if the condition is satisfied else, shows the text “Target not Achieved.” However, the execution of code displays the following error message:

 #!/bin/bash
num1=15;
num2=8;
sum=$(($num1+$num2));
if [ $sum>20 ]; then
echo "Target Achieved!"
else
echo "Target not Achieved."

Let’s check how it behaves once executed:

The error itself shows the syntax error.

Solution: Correct the Syntax (Close the if Condition)

The code shows that the “if” condition is opened but never closed. As for every “then” keyword of the if condition, the keyword “fi” is used to close that condition. The code shows that the if condition is open but not closed using the “fi” keyword, so the error can be removed after correcting the syntax error and putting the “fi” at the end of the if condition.

Let’s put the missing “fi” keyword and check its output:

#!/bin/bash
num1=15;
num2=8;
sum=$(($num1+$num2));
if [ $sum>20 ]; then
echo "Target Achieved!"
else
echo "Target not Achieved."
fi

Run the script as follows:

The output shows the “if” condition result “Target Achieved!”, which verifies the error is removed.

Example Scenario: Bracket is Missing

Keeping the above code, now we have missed putting the bracket while calculating the sum of the two variables.

#!/bin/bash
num1=15;
num2=8;
sum=$(($num1+$num2);
if [ $sum>20 ]; then
echo "Target Achieved!"
else
echo "Target not Achieved."
fi

Execute the script:

The error shows there is something wrong with the syntax.

Solution of Example Scenario: Insert the Missing Bracket

Let’s correct the error by putting the “)” and executing it. The below code is corrected by putting the missing “)”, let’s execute it:

#!/bin/bash
num1=15;
num2=8;
sum=$(($num1+$num2));
if [ $sum>20 ]; then
echo "Target Achieved!"
else
echo "Target not Achieved."
fi

Now, the script is executed:

The error-free output shows the syntax error is removed.

Similarly, if you are getting this error, you must check the syntax you have followed in writing a program.

That’s the end.

Conclusion

The error “syntax error: unexpected end of file” occurs due to the mismatched structure of the code (incorrect syntax). Examples of it are missing the bracket or not closing the condition (after which the file is terminated). To counter this, you need to look at the syntax and fix all the errors to solve “syntax error: unexpected end of file”.