LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2009, 07:33 AM   #1
gr8linux
Member
 
Registered: May 2009
Posts: 44

Rep: Reputation: 15
Post echo with \r


what is the meaning of carriage return? echo has the option \r in man page.... but i didnt get it..... plz clear out this by some examples..
 
Old 05-12-2009, 07:37 AM   #2
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,629

Rep: Reputation: Disabled
CR is usually also referred to as Enter key on your keyboard.
 
Old 05-12-2009, 08:09 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
"Carriage Return" is the lever on a typewriter that returns the (paper) carriage to the home position (start typing at the left of the page.) Returning the carriage normally involves also advancing one line.

Look at this series of tests with echo:
Code:
mherring@Ath:~$ echo -e "rabbit\rdog"
dogbit

mherring@Ath:~$ echo -e "rabbit\ndog"
rabbit
dog

mherring@Ath:~$ echo -e "rabbit\tdog"
rabbit  dog

mherring@Ath:~$ echo -e "rabbit\bdog"
rabbidog
Note first that "carriage return" (\r) does not advance one line--it simply moves back to the beginning of the line.
"newline" (\n) seems to do what the typewriter lever does---i.e. it moves to the beginning of the line and advances one line.

If you look at a simple text file with a hex editor, you'll see that "newline" (\n, ascii code 0x0a) is used and not "carriage return".

In typing text, the "enter" key inserts a newline (at least in Unix/Linux).
 
Old 05-12-2009, 08:36 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,938

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
History 101 ... the carriage held the paper in the typewriter. It moved as you typed so when you were at the other side of the page a mechanical lever would be used to push the carriage back to the beginning and advance one line. That lever was called the carriage return (CR). The term is a carryover from the old typewriter days. There was also a knob on the side to move the paper from top to the bottom as necessary for corrections and formatting.

http://en.wikipedia.org/wiki/Typewriter

If you look at the picture the top of the typewriter that says Underwood is the carriage.
http://images.google.com/imgres?imgu...a%3DX%26um%3D1

Welcome to LinuxQuestions
 
Old 05-13-2009, 01:52 AM   #5
gr8linux
Member
 
Registered: May 2009
Posts: 44

Original Poster
Rep: Reputation: 15
Smile

[QUOTE=pixellany;3538135]"Carriage Return" is the lever on a typewriter that returns the (paper) carriage to the home position (start typing at the left of the page.) Returning the carriage normally involves also advancing one line.

Look at this series of tests with echo:
[CODE]mherring@Ath:~$ echo -e "rabbit\rdog"
dogbit


Note first that "carriage return" (\r) does not advance one line--it simply moves back to the beginning of the line.




thanx for ur response, as u said it simply moves back to the beginning of the line,if it is so then it should print dograbbit........ can u plz,
gv me some more examples of \r.....
 
Old 05-13-2009, 02:02 AM   #6
gr8linux
Member
 
Registered: May 2009
Posts: 44

Original Poster
Rep: Reputation: 15
Smile

Quote:
Originally Posted by michaelk View Post
History 101 ... the carriage held the paper in the typewriter. It moved as you typed so when you were at the other side of the page a mechanical lever would be used to push the carriage back to the beginning and advance one line. That lever was called the carriage return (CR). The term is a carryover from the old typewriter days. There was also a knob on the side to move the paper from top to the bottom as necessary for corrections and formatting.

http://en.wikipedia.org/wiki/Typewriter

If you look at the picture the top of the typewriter that says Underwood is the carriage.
http://images.google.com/imgres?imgu...a%3DX%26um%3D1

Welcome to LinuxQuestions




can u plz gv me some examples of \r with echo.... to clear it out.
 
Old 05-13-2009, 03:01 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by gr8linux View Post
can u plz gv me some examples of \r with echo.... to clear it out.
I already gave you several examples!! You can make your own variations.....

Sorry if I was too subtle---part of the point was that \r in Linux is NOT the same as the carriage return on a typewriter.

Quote:
thanx for ur response, as u said it simply moves back to the beginning of the line,if it is so then it should print dograbbit
No..."\r" puts you back at the beginning of the line and starts entering data at that point. The existing data is over-written. The insertion (and shifting) that you would see in a word processor takes a more sophisticated function.
 
Old 05-13-2009, 08:19 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Here's an extract from the ASCII table:
Quote:
Decimal Octal Hex Binary Value
------- ----- --- ------ -----


010 012 00A 00001010 LF (Line Feed)
011 013 00B 00001011 VT (Vertical Tab)
012 014 00C 00001100 FF (Form Feed)
013 015 00D 00001101 CR (Carriage Return)
As you can see, (using decimal codes)
10 = Line feed (\n) ie move printhead/cursor down one line
13 = Carriage Return (\r) ie move printhead/cursor back to start of line

In MS systems, they use \r\n to start a 'newline', as the old tech did.
However, it was decided in Unix that we would just go with \n and take the \r as assumed. This is what causes issues if you move text files between the two systems eg see dos2unix tool.
However it was possible with typewriters to just do a \r and overtype the same line, as you can also in *nix.
You really need to bookmark a copy of the ASCII table eg http://www.asciitable.com/ and write a program to test out the various ctrl chars (anything where decimal code is < 32) so you understand.
 
Old 05-14-2009, 03:04 AM   #9
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
Not forgetting that apple mac's use just the carriage return and take the line feed as assumed

(At least they used to, not sure what they do now)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 09:25 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 03:14 PM
Echo onelung02 Slackware 6 08-26-2005 05:50 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 03:59 PM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 02:13 PM


All times are GMT -5. The time now is 08:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration