Why Do Windows and Linux Scripts Fail? Master Line Ending Differences
This guide explains how Windows and Linux handle line‑ending characters differently, the visual and functional problems caused by mismatched formats, and provides practical methods and tools to view and convert files so scripts run correctly across both operating systems.
Format Differences
End‑of‑line (EOL) characters mark the end of a text line; the actual code varies by OS.
Windows/DOS uses CR+LF (\r\n), Unix/Linux and macOS use LF (\n), classic Mac OS uses CR (\r).
CR is ASCII 0x0D (\r), LF is ASCII 0x0A (\n); DOS shows CR as ^M when viewed on Unix.
Example script test-dos.sh:
#!/bin/bash
echo "Hello World !"Impact of Format
Visual Impact
Opening a Unix/Mac file in Windows may display all text on a single line.
Opening a Windows file in Unix/Mac often shows an extra ^M at line ends.
Functional Impact
Shell or Python scripts created on Windows may fail on Linux with bad interpreter: No such file or directory errors caused by ^M.
During make, a missing mksh may be reported because of the wrong line ending.
-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory
make[3]: ./mksh: Command not foundViewing Format
Windows
Editors such as VS Code, UltraEdit, Notepad2 show CR+LF for Windows format or LF for Linux format.
Notepad++ can search for \r\n to identify DOS format.
Linux
cat -vdisplays ^M at the end of each line. cat -T shows tabs as ^I. od -c and hexdump -c reveal the raw byte sequence.
Vim status bar indicates file format, e.g., "test-dos.sh" [noeol][dos] 2L, 33B.
cat -v test-dos.sh
#!/bin/bash^M
echo "Hello World !"^M od -c test-dos.sh
0000000 # ! / b i n / b a s h \r
e c h
0000020 o " H e l l o W o r l d !Modifying Format
Windows
In VS Code, click the CRLF indicator in the status bar and switch to LF.
Notepad2 offers a “Line Endings” menu to change the format.
Notepad++ can replace \r\n with \n using Find & Replace.
Linux
Special Tools
In Vim, execute :set ff=unix or :set fileformat=unix to convert to Unix format. dos2unix converts DOS files to Unix; unix2dos does the opposite. fromdos (part of tofrodos) converts DOS to Unix.
dos2unix test-dos.sh
# converts in‑place
dos2unix -n test-dos.sh test-unix.sh
# creates a new Unix‑formatted fileText‑Processing Tools
sed 's/^M//' test-dos.sh > test-unix.shremoves CR characters from a single file.
Convert many files: find ./ -type f -print0 | xargs -0 sed -i 's/^M$//' In Vim: :%s/^M//g (enter Ctrl+V M for ^M) then :wq. tr -d "\015" test-dos.sh > test-unix.sh deletes CR characters.
Perl one‑liners: cat test-dos.sh | perl -pe 's/\r//g' > test-unix.sh or perl -p -e 's/\r\n/\n/g' test-dos.sh.
sed 's/^M//' test-dos.sh > test-unix.sh
find ./ -type f -print0 | xargs -0 sed -i 's/^M$//'
tr -d "\015" test-dos.sh > test-unix.sh
perl -pe 's/\r//g' test-dos.sh > test-unix.shSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
