please explain the difference between Carriage return (`r) and New line (`n) in powershell

Gopi Raju
Gopi Raju used Ask the Experts™
on
Can someone please explain the difference between Carriage return (`r) and New line (`n) in powershell.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlexSenior Infrastructure Analyst

Commented:
The /r stands for "return" or "carriage return" which owes it's history to the typewriter. A carriage return moved your carriage all the way to the right so you were typing at the start of the line. The /n stands for "new line", again, from typewriter days you moved down to a new line.

I did just pull that from google, pretty sure it's right :D
ste5anSenior Developer

Commented:
They are two different non-printable characters in the ASCII table... usually used as text line terminator. The names come from the type write as already explained.

And because printers and modern computers are no type writers, we have a slight disagreement in the community, what the correct text line terminator should be:

CR LF is used by Windows, thus the original typewriter sequence to get the first position in the new line.
LF is used by Unix-style OS and MacOs.

Rumors tell that the LF was used to save a byte back in those days.

btw, LF is 0xA0 (10d) and CR is 0xC0 (13d)
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT

Commented:
When I was studying 9th standard learned type writing (without using computer keyboard)
Steps followed using that typewriter:
type following characters (without space)
a
s
d
f
g
f
;
l
k
j
h
j
There was no Enter key in that typewriter.
it was required to move the carriage to the beginning of the line (\r)
and rotate that carriage one time(\n) to go the new line.

This is what is happening if you use C:\Windows\System32\notepad.exe
Typing following characters
Testing Press Enter
Save and close notepad.exe
Internally this is saved as:
Sample output of that file:
$ /bin/cat 29108662.txt
testing

Open in new window


$ /usr/bin/file  29108662.txt
29108662.txt: ASCII text, with CRLF line terminators

Open in new window


$ /usr/bin/od -bc  29108662.txt
0000000 164 145 163 164 151 156 147 015 012
          t   e   s   t   i   n   g  \r  \n
0000011

Open in new window

0000000 being offset (octal) value
At position zero t
position 1 e
position 2 s
position 3 t
position 4 i
position 5 n
position 6 g
position 7 \r
position 8 \n
No character at position 9
Octal value of 9 is 11 => 0000011

This is how it is happening when using CRLF (carriage return and line feed)
at Linux oriented operating systems.
$ echo "AIX CYGWIN_NT HP-UX Linux Linux UNIX powershell" | /usr/bin/tr " " "\n" | /bin/sort -u
AIX
CYGWIN_NT
HP-UX
Linux
UNIX
powershell

Open in new window

new lines used to be written without carriage return(\r)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial