Fileformat and the impacts in bash scripts

I had the problem calling a bash script I got the error:
“syntax error: unexpected end of file”
and
“: command not foundne 2: ”

I created a simple scritp for demonstration

#!/bin/bash

echo "hello"

In this sample we have following error after execution: “: command not foundne 2: ” As you can see, there is nothing on line 2.

The solution was as simple as the problem heavy.  Just execute the following command on the linux system.


dos2unix myScript.sh myScript.sh

And the script will work fine.

The easiest way to spot that the file format is wrong would be:


 head myScript.sh | od -c

 0000000   #   !   /   b   i   n   /   b   a   s   h  \r  \n  \r  \n   e
 0000020   c   h   o       ”   h   e   l   l   o   ”
 0000033

So you can see the newlines are done with (\r\n). This can occur if you write the script under windows and moved it to a linux system. After executing dos2unix the head will look like this:

 0000000   #   !   /   b   i   n   /   b   a   s   h  \n  \n   e   c   h
 0000020   o       ”   h   e   l   l   o   ”
 0000031

Every “linefeed carriage return” is replaced with a simple “linefeed”.

About the author

Adrian Elsener

Add comment

Recent Posts