\r character in shell script

Solution 1:

Your problem is that the file has Windows line endings. This can be caused by editing a file in Windows and trying to run it on a non-Windows system.

You can fix this problem using dos2unix to convert the line endings:

dos2unix ConstruedTermsXMLGenerator.sh

The corresponding utility to convert in the other direction is unix2dos.

Some systems have fromdos and todos.

Solution 2:

You can use sed -i 's/\r$//' scriptname.sh

Replace the scriptname with actual script name.

Solution 3:

I used notepad++ to convert the line endings.

Edit > EOL Conversion > UNIX/OSX Format

Solution 4:

I had the same error and what I did was to transform the characters '\r' to '\n'. using this line:

tr '\r' '\n' < oldfile.sh > newfile.sh
mv newfile.sh oldfile.sh
chmod +x oldfile.sh
./oldfile.sh

I think you could also delete the '\r' characters by using:

tr -d '\r' < oldfile.sh > newfile.sh

tr is the command trasnform, and the -d is delete the following character.

I think the shell actually doesn't like '\r' character.

Solution 5:

I had this exact issue when creating a .sh file on a Mac (unix) and executing it in Linux. Turns out that I had to set FileZilla FTP settings to 'Binary' transfer type:

  • "Settings>Transfers>File Types>Default transfer type" to "Binary" (instead of "Auto")