Ubuntu – Unexpected EOF in a bash script, why

bashcommand linescripts

I can't normally run sh scripts. It's confusing. I set the permissions to 777 and +x so it should work with the 'sh' command, right…

But it's working very strange. For example this scripts works ok:

echo "hello"

it prints "hello" with no errors. But this script:

#!/bin/bash
for i in `seq 1 5`; do 
    echo $i
done

…outputs this error:

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

Is there something wrong with that loop?

Best Answer

The problem it is, that you probably made a Ctrl+C Ctrl+V from a website, and the end-of-line char there was something strange, not printable character. (You can make it visible by dumping to hex the file.)

Please open your file in an editor, and retype everything manually, or at least remove the last eol characters, save your work, and try again.

Related Question