"$'\r': command not found" Is Displayed When the .sh Script Is Executed

Symptom

When the .sh script is executed, the error message "$'\r': command not found" is displayed.

Cause Analysis

In Windows OS, the newline character is \r\n. In Linux, the newline character is \n. If the .sh script contains newline characters of Windows OS and is directly uploaded to the Linux OS for execution, the \r characters cannot be identified by the Linux OS. As a result, the error message "$'\r': command not found" is displayed.

Solution

Run the sed -i 's/\r//' *.sh command on Linux to delete \r from the .sh script.