Interview Prep/Shell Scripting/Shells, scripts and execution

How does Bash handle a script with Windows line endings (CRLF)?

IntermediateConceptShells, scripts and execution
Answer

Every line ends in \r, which Bash treats as part of the last word. The shebang becomes bash\r, so ./s.sh fails with bad interpreter: No such file or directory, and bash s.sh fails with $'\r': command not found or syntax error near unexpected token $'\r'.

file deploy.sh            # "with CRLF line terminators"
sed -i 's/\r$//' deploy.sh   # or: dos2unix deploy.sh

Prevent it with *.sh text eol=lf in .gitattributes.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Shells, scripts and execution