I have a simple shell script I copied from a working script. It works if I copy-paste it to a terminal:
if true
then
true
fi
However, when I run the script with bash myscript, I get various syntax errors as if some of the keywords are missing.
myscript: line 4: syntax error near unexpected token `fi', as ifthenisn't there.myscript: line 6: syntax error: unexpected end of file, as iffiisn't there.myscript: line 4: syntax error near unexpected token `$'\r'.. what?
Why does this happen in this particular script, but not on my command line or in the script I copied from?
$'\r'which suggests this is the problem. See How to convert DOS/Windows newline (CRLF) to Unix newline (\n) in bash script? amongst many other relevant questions.