18

I have a script that sometimes prints a newline as \n and sometimes uses \r\n. What is the difference between the two, and is one preferable over the other?

David542
  • 104,438
  • 178
  • 489
  • 842
  • possible duplicate of [\r\n , \r , \n what is the difference between them?](http://stackoverflow.com/questions/15433188/r-n-r-n-what-is-the-difference-between-them) – Games Brainiac Jan 30 '15 at 02:46

2 Answers2

19

"\n" is the class Unix/linux style for new line. "\r\n" is the default Windows style for line separator. "\r" is classic Mac style for line separator. I think "\n" is better, because this also looks good on windows, but some "\r\n" may not looks so good in some editor under linux, such as eclipse or notepad++. If you are handling some protocols, the "\r\n" is usually required.

Cui Heng
  • 1,265
  • 8
  • 10
  • notepad++ is a Windows-only editor; there is no native Linux version. Apart from that, "may not look so good" is highly unspecific. – mara004 Dec 15 '22 at 13:07
  • I find that [this answer](https://stackoverflow.com/a/15433225/15547292) is more concise. – mara004 Dec 20 '22 at 18:34
8

Code

print "\n",
print "\r",
print "\r\n",

gives you the following output in hex

0a 0d 0d 0a

See also answers here and here.

Community
  • 1
  • 1
dmitri
  • 3,183
  • 23
  • 28