Text files or scripts with Windows carriage returns are often needed to be removed when running a script within Linux. Once the file is on Linux, the sed command can remove the Windows-specific carriage returns. This is useful for script files such as bash files, that may have been created on a Windows OS first.
Access to the Linux command line and a file that contains Windows carriage returns.
Once you have located the file in Linux in the filesystem, you can remove the carriage returns with sed. sed stands for “Stream Editor” and should already be in the Linux path. You can do file processing with this tool on the standard input and files. You can also do pattern matching, like grep. To remove the carriage returns \r that Windows generates, you may use the following:
sed -i 's/\r//g' FileWithCarriageReturns.sh
The sed command has the operation within single quotes. The shell or a script running this line will not be able to access this area. If there were a variable within it which the shell or a script might recognize, it cannot replace it with a value. The s within the quotes stands for substitution. The first set of ‘//‘ within the quotes is what is being matches. The third ‘/‘ is what the pattern will be replaced by. In this instance, ‘\r‘, the carriage return, is being replaced by nothing (between the second and third /), so it is being removed. The g will look for all occurrences.
from the command line on Linux, to view more information about the sed command.