14

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
  • 101,766
  • 154
  • 423
  • 727
  • 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

17

"\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,217
  • 8
  • 10
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,047
  • 20
  • 27