The script has Windows (CRLF) line endings
CommonEdited or created on Windows, checked out by Git with core.autocrlf converting to CRLF, or pasted from a Windows tool.
Confirm
file script.sh; head -1 script.sh | od -c | head -2
with CRLF line terminators, or \r before \n on the first line
Fix
- Strip the carriage returns: sed -i 's/\r$//' script.sh on Linux, sed -i '' $'s/\r$//' script.sh on macOS, or dos2unix script.sh if it is installed.
- Stop it recurring: add *.sh text eol=lf to .gitattributes and commit, so Git stores and checks out LF whatever each person's settings.
printf '*.sh text eol=lf\n' >> .gitattributes && git add .gitattributes && git add --renormalize -- '*.sh' && git commit -m 'Shell scripts use LF'
Reproduced on git 2.50.1: the repository then stores LF and a fresh clone runs. Limit --renormalize to the scripts: with . it also stages any other edited file. Once that commit is made, refresh your own copy with rm script.sh && git checkout -- script.sh; uncommitted edits to that file are lost.