This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Why my files ended with ^M ?? (shell/bash)

I have a file.sh with the next commands:

#!/bin/bash
sed 1q file.txt > file_0.txt
grep -w "name" file.txt > file_1.txt
cat file_0.txt file_1.txt > final_file.txt

When I run: sh file.sh I obtain this error:

cat: file_0.txt: No such file or directory
cat: file_1.txt: No such file or directory

In the last command (cat) the files are not recognized because the final names are:

file_0.txt^M
file_1.txt^M

Why outputs end with ^M ? How can I solve that?

shell linux cat grep bash

Somewhere during their journey, the files visited windows.

2 answers

That is the carriage return character (LINK). Use dos2unix util to remove.

Finally I solved that with the next command:

sed -i 's/\r$//' file.sh

Log in to answer this question.