Unexpected end of file error – Bash Script / Shell Syntax Error

Shell Syntax Error Unexpected end of file. What Does This Error Mean? Normally you got this error while writing the script on Windows.

But Why? Because Windows breaks lines in text files with a combination of two characters, Carriage Return and Line Feed (also known as CRLF). Unix or Linux, on the other hand, only uses the Line Feed character to break lines.

How to fix Bash Unexpected End of File Error – Step by Step Guide

Here are the some possible things that you can do to sort out this unexpected eof error.

Answer 1: 

  • It is most likely a syntax error.
    • Some expression is missing a closing brace.
    • Some string is missing a quote or has one too many quotes.
    • An if has no fi
    • A for or while or select has no do and done
    • A case has no esac
    • A semicolon is needed between statements
    • An expression is not spaced or delimited with a newline.
    • Often a semicolon is not separated from the previous expression by a space and so the expression is not evaluated correctly.
  • The file being read is DOS format, so the CRLF needs to be translated with tr to unix line endings.
    • cat "<oldfile>" | tr -d -'\15\32' > "<newfile>"
    • dos2unix -n in.txt out.txt
  • else if (wrong) used instead of elif (right)
  • An error in a config file similar to the above .bashrc, .bash_profile, .etc .

Answer 2:

iF You are running a Bash script, and you see a syntax error: Unexpected end of file. it means you created your script using Windows.

Because Windows uses a combination of two characters, Carriage Return and Line Feed, as line break in text files (also known as CRLF).

On the other side Unix (or Linux) only use the Line Feed character as line break. So, let’s see what happens if we save a script using Windows and then we execute it in Linux. Using the Windows notepad I have created a Bash script called end_of_file.sh:

#/bin/bash

if [ $# -gt 0 ]; then

echo “More than one argument passed”

else

echo “No arguments passed”

fi

And here is the output I get when I execute it:

[ec2-user@localhost scripts]$ ./end_of_file.sh

./end_of_file.sh: line 2: $’\r’: command not found

./end_of_file.sh: line 8: syntax error: unexpected end of file

How do we see where the problem is?

Edit the script with the vim editor using the -b flag that runs the editor in binary mode:

[ec2-user@localhost scripts]$ vim -b end_of_file.sh

(Below you can see the content of the script)

#/bin/bash^M

^M

if [ $# -gt 0 ]; then^M

echo “More than one argument passed”^M

else^M

echo “No arguments passed”^M

fi^M

At the end of each line we see the ^M character. What is that?

It’s the carriage return we have mentioned before. Used by Windows but not by Unix (Linux) in line breaks.

To solve both errors we need to convert our script into a format that Linux understands.

The most common tool to do that is called dos2unix.


Answer 3:

Unexpected eof is the second error. You need to fix the previous error where it states that it was looking for a close quote. It tells you which line has a single quote or double quote with no matching close quote. In this test, line 2 has a single quote.

$ Desktop/test.sh
Desktop/test.sh: line 2: unexpected EOF while looking for matching `”
Desktop/test.sh: line 6: syntax error: unexpected end of file   


Solved “syntax error: unexpected end of File” in Linux Shell Scripting

In this section we will see how to solve “syntax error: unexpected end of File” in Linux Shell Scripting.

While calculating sum of two numbers, I got an error while running the script which was totally unexpected. The error is minor but it is so common that anyone can face this issue.

Then I decided to write a short article about this, in which I would explain the different things that can cause an unexpected end of file error.

I was working on a simple shell script, which is shown below. Here, I’m adding the values of a and b and putting the result in c. The output was then shown with echo $c, as you can see below.

ursol@ubuntu:~$ nano calculate
#! /bin/bash

a=5
b=7

sum()
{
c=$(($a+$b))
echo $c
}

sum()

When I tried to run above script, it gave me below error output.

ursol@ubuntu:~$ ./calculate
./calculate: line 13: syntax error: unexpected end of file

Unexpected end of file error can happen for a number of reasons, but most of the time it’s because either the parentheses or the braces weren’t opened or closed correctly. The unexpected end of file error means that you are trying to open something that was never closed, like bash of braces. Use bash -x script name> if you want to find out more about the error. Here, the -x switch is used to help with debugging. This is more helpful when you have a long script that is hard to find mistakes in.

Since it’s a pretty small script in our case, it’s easy to find the mistake. So, if you look at the output, you’ll see that it says the error is on line 13, which is highlighted below.

ursol@ubuntu:~$ nano calculate
#! /bin/bash

a=5
b=7

sum()
{
c = $(($a+$b))
echo $c
}

sum()

You may have found the mistake by now. In line 13, the sum function has been called by using open and close parentheses, which is wrong. So, if you remove the opening and closing parentheses, as shown below, and then run the script, you’ll see that the sum function works as it should.

ursol@ubuntu:~$ nano calculate
#! /bin/bash

a=5
b=7

sum()
{
c=$(($a+$b))
echo $c
}

sum

Output

ursol@ubuntu:~$ ./calculate
12

The other thing that could happen is that you open the braces but forget to close them again, like in the example below, where you opened the braces in line 12 but forgot to close them again. When you try to run the script, you will also get an unexpected end of file error. So, you have to be careful when you close your braces.

ursol@ubuntu:~$ nano calculate
#! /bin/bash

a=5
b=7

sum()
{
c=$(($a+$b))
echo $c

sum

Output

ursol@ubuntu:~$ ./calculate
./calculate: line 12: syntax error: unexpected end of file


Unexpected EOF while looking for matching

I just wrote a bash script and always getting this EOF-Error.

So here is my script (only works on OS X):

eof file of matching

unexpected eof while looking for matching

unexpected eof while looking for matching

eof

And here is my error:

./coding.sh: line 144: unexpected EOF while looking for matching `”‘
./coding.sh: line 147: syntax error: unexpected end of file

Answer

You can see your problem if you just look at your question. Note how the syntax highlighting is messed up after line 95:

echo -e “Sorry, an error occurred. You have to run this on OS X””

As the error message tells you, you have an unmatched “.
Just remove the extra ” from the line above and you should be fine:

echo -e “Sorry, an error occurred. You have to run this on OS X”

Hope the above explanation makes sense and helps you fix the error if you are also getting an unexpected end of file error. Please tell me what you think about our article in the box below!!


Frequently Asked Questions

What does Unexpected end of file mean in bash?

An Unexpected end of file error in a Bash script usually means that the structure of the script is wrong somewhere. When you try to run your Bash script, you will get an error if you forget to close your quotes or end an if statement, a while loop, or something else.

What does end of file was reached unexpectedly mean?

If you don’t have enough storage space, you may get the message “could not finish your request because an unexpected end-of-file was found.” If so, you should do what you can to get more space on your disc.

What is unexpected $end?

This error is typically caused by a missing } used in PHP to denote content belonging to a WHILE, IF, or FOR loop. You might accidentally comment out a } when you comment out a line of code.

How do I fix unexpected EOF in Python?

You have to pay attention to the parameters and how they are written. Need to check all of the functions and the statements that end them.
Before running the programme, please make sure that all of the functions’ parameters are set.
Also, check and fix the way the programme is spaced out.

What is unexpected end of file in PHP?

When a file doesn’t have the right closing tags, it can cause an error called “Unexpected End of File.” This error can sometimes look like the “white screen of death” or a “500 error.” In this article, we’ll talk about what “Unexpected End of File” errors are and how to fix them.

What is unexpected end of file from server?

Unexpected end of file from server” usually means that the server you are calling isn’t responding correctly. In other words, it takes the server a long time to process the data, so the connection times out before the server can send a response. It could happen on either the server or the client side.

How do I fix gzip Stdin Unexpected end of file?

The error indicates a corrupted installation package.

Once you’ve confirmed that the installation package is corrupted:
  1. Delete the installation package you downloaded.
  2. Re-download the installation package from the Download Center.
  3. Check if the download was successfully completed by comparing the file sizes once more

Latest Posts

Follow me
Latest posts by Muhammad Usman Shafique (see all)

Was this post helpful?

Yes
No
Thanks for your feedback!