Why my files ended with ^M ?? (shell/bash)
2
1
Entering edit mode
5 weeks ago
nlt_220593 ▴ 10

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 • 221 views
ADD COMMENT
2
Entering edit mode

Somewhere during their journey, the files visited windows.

ADD REPLY
4
Entering edit mode
5 weeks ago
GenoMax 102k

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

ADD COMMENT
0
Entering edit mode
5 weeks ago
nlt_220593 ▴ 10

Finally I solved that with the next command:

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

Login before adding your answer.

Traffic: 2498 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6