Bash syntax error unexpected end of file

Welcome to, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

Are you new to Visit the following links:
Site Howto | Site FAQ | Sitemap | Register Now

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux — A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author’s experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Betcha I missed a fi somewhere.

(basically, it tars whatever you pass it, but i’ve overcomplicated it.)

not sure if this is helpful, but:

1 Answer 1

You have not closed two if statements with fi :

Forgive me for this is a very simple script in Bash. Here’s the code:

after running sh

syntax error: unexpected end of file

17 Answers 17

I think is with CRLF line terminators.

then the problem will be fixed.

You can install dos2unix in ubuntu with this:

Another thing to check (just occured to me):

  • terminate bodies of single-line functions with semicolon

I.e. this innocent-looking snippet will cause the same error:

To make the dumb parser happy:

/.git-completion.bash fi will produce that error, whereas if [ -f

/.git-completion.bash; fi; will not. notice the little semi-colons at the end, ie: after .bash and fi . – Emmanuel Mahuni Aug 27 ’18 at 7:51

i also just got this error message by using the wrong syntax in an if clause

  • else if (syntax error: unexpected end of file)
  • elif (correct syntax)

i debugged it by commenting bits out until it worked

an un-closed if => fi clause will raise this as well

tip: use trap to debug, if your script is huge.

on cygwin I needed:-

in .bash_profile . This way I didn’t need to run unix2dos

Open the file in Vim and try

Convert eh line endings to unix endings and see if that solves the issue. If editing in Vim, enter the command :set fileformat=unix and save the file. Several other editors have the ability to convert line endings, such as Notepad++ or Atom

So I found this post and the answers did not help me but i was able to figure out why it gave me the error. I had a

The issue was that i copied the above code to be in a function and inadvertently tabbed the code. Need to make sure the last EOF is not tabbed.

This was happening for me when I was trying to call a function using parens, e.g.

I was able to cut and paste your code into a file and it ran correctly. If you execute it like this it should work:

Note that "" must be executable:

You may be getting that error b/c of how you’re doing input (w/ a pipe, carrot, etc.). You could also try splitting the condition into two:

Or, since you’re using bash , you could use built-in syntax:

And, finally, you could of course just check if 3 arguments were given (clean, maintains POSIX shell compatibility):


In my case, I was working on Windows OS and I got the same error while running autoconf.

    I simply open file with my NOTEPAD++ IDE.

Then I converted the File with EOL conversion into Windows (CR LF) as follows:


I had the problem when I wrote "if — fi" statement in one line:

Write multiline solved my problem:

I just cut-and-pasted your example into a file; it ran fine under bash. I don’t see any problems with it.

For good measure you may want to ensure it ends with a newline, though bash shouldn’t care. (It runs for me both with and without the final newline.)

You’ll sometimes see strange errors if you’ve accidentally embedded a control character in the file. Since it’s a short script, try creating a new script by pasting it from your question here on StackOverflow, or by simply re-typing it.

What version of bash are you using? ( bash —version )

[an error occurred while processing the directive]
Карта сайта