I ran into this problem while building a Docker Image:
$'\r': command not found
As part of this docker image, a script was run, and the error came from running that script. The issue seemed to be the line endings of the file in the script. The file had windows line endings, which contain a Carriage Return and a Line Feed instead of just the Line Feed, which is the common Unix approach.
There are a handful of different ways to solve this issue, but what I did was open up the sh file in IntellIJ
To change the line endings in IntelliJ, you can do so in the bottom right corner.
Once that was done, I could save the file, and rebuild the docker image without error.