We have 10 PC with some version of Ubuntu and only remote access. While doing some upgrades to custom software I did not notice that the line endings in some scripts were Windows version (CR+LF) and not the Unix version (LF). So now when I want to launch the script it gives an error:
bash: /usr/local/bin/portsee: /usr/bin/python^M: bad interpreter: No such file or directory
Is there a way to change all line endings in a script from terminal. The thing is that I can not install any new software to this group of PC-s.
Option 1:
dos2unix
You can use the program
dos2unix
, which is specifically designed for this:will replace all CR from all lines, in place operation.
To save the output in a different file:
You might need to install it first by:
Option 2:
sed
Or you can use
sed
to replace all CR (\r
) from line endings:With option
-i
, the file will be edited in-place, and the original file will be backed up asfile.txt.bak
.The
sed
solution is not portable to all platforms. It didn't work for me on macOS unless I didbrew install gsed
and usedgsed 's/\r$//'
.For a solution that works in most places without installing anything, I use
To edit a file in-place, I produce the new data in a subshell before erasing and overwriting the original file: