user1390445
user1390445

Reputation: 41

File with Control-m characters

we are working on windows machine and code is deployed in linux.

Some how while developing some linux scripts we have some ctrl-m character. release is provided to customer.

what can be the impact on ctrl-m characters in shell scripts.

nus

Upvotes: 3

Views: 18822

Answers (5)

Willy
Willy

Reputation: 9881

You could use the following script to clean up ^M from files in a directory.

for file in $(find path/to/your/files -type f); do
  tr -d '\r' <$file >temp.$$ && mv temp.$$ $file
done

Upvotes: 0

prasankn
prasankn

Reputation: 181

You can use the below syntax too. Since in windows, this Control+v+m will not work , you can use \r instead.

Open the file in binary mode to view the Control-M characters using vim editor with -b option and use the below command to replace the Control-M characters

%s@\r@@g

Upvotes: 0

Neeraj
Neeraj

Reputation: 1173

open the file in vi editor

press escape and type below on command line in the file.

:%s/^M//g

make sure ^M is added by hitting Cntrl key + V + M then hit enter

it will remove the characters and you can save the file.

Upvotes: 11

Adam Liss
Adam Liss

Reputation: 48320

The ^M characters are carriage returns. Windows and DOS terminate lines of text with CR (^M, or ASCII code 13) followed by LF (^J, or linefeed, ASCII code 10). Linux uses just LF, so the carriage returns appear as part of the text and are interpreted as such. That means they'll break scripts.

Most linux distributions come with a utility called dos2unix pre-installed. You can use it to convert the end-of-line characters from DOS style to linux style.

Some editors will do the conversion automatically for you; others have options that let you specify which mode to use.

Upvotes: 5

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799150

It will change the shebang line, so that the interpreter isn't found.

$ ./t.sh
bash: ./t.sh: /bin/bash^M: bad interpreter: No such file or directory

Upvotes: 5

Related Questions