Syntax error near unexpected token `elif'

Two ways to resolve this

1) Using Sed:-

Syntax

sed -i 's/\r//' filename.txt

2) Using dos2unix command

Syntax

dos2unix fileName.txt fileName.txt

It's your line endings. Transferring it from Windows has left the CR/LF line endings on.

When I create a script then manually add the CR characters, I get exactly the same error:

qq.sh: line 3: syntax error near unexpected token `elif'
'q.sh: line 3: `elif [ 1 == 1 ] ; then

You can fix it by removing the CR character from CR/LF line endings.

cat script.sh | sed 's/\r$//' >newscript.sh

Tags:

Shell

Bash