Solved the error - Syntax error end of file unexpected (expecting then)

8/01/2022 0 Comments

While running your (Bash) Shell Script have you seen the below error? 

Syntax error: end of file unexpected (expecting "then")

You may think that you have written your Bash script perfectly fine but the below error makes you wonder where the error actually is? This happens when the file is edited in Windows and the editor does not have an option to save files in "Unix" file format. The shell is seeing the word then^M with a carriage return on the end.

Here is the fix for it. From the prompt execute the following command:

sed -i 's/\r$//' filename where filename is the name of your bash script file.

This will ensure that the line ending is proper and executing your file with the sh command will execute your Bash script perfectly well. 

Enjoy shell scripting!!!



0 comments:

Blog Archive