Syntax error: "fi" unexpected (expecting "then") in bash script

Most probably this is because carriage-return \r in your script. Try run this command to clean-up your script. Just run once. Original file will be backed up.

perl -pi.bak -e 's/\r$//' /home/pi/sh/test.sh

You can try

$ dos2unix /home/pi/sh/test.sh

and run it again.


if xxx then
  commond
fi

Syntax error: “fi” unexpected (expecting “then”)

try it :

if xxx 
then
  commond
fi

it's ok.


If you are editing the script file with Notepad++ on windows you can convert the EOL from the program menu with

Edit => EOL Conversion => Unix (LF)