Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Insert carriage return, line feed into Terminal UNIX

The issue of line endings in OS X can be complicated since UNIX uses a line feed (LF, 0x0A in ASCII) for an end-of-line, and Macintosh typically uses a carriage return (CR, 0x0D in ASCII). In the Terminal on OS X though, pressing Return will generally send an LF.

Figuring out how to send a CR to the Terminal instead can be quite difficult, since searching for information on control characters, escape characters, the ASCII set, end of lines, CR's, LF's and CRLF's quickly becomes confusing! I hope this tip saves someone else the hours it took me to figure it out, because the solution is very simple.

To send an LF to the Terminal, press Return. You should get a new line, depending on the application you have running.

To send a CR to the Terminal, press Control-V, then Return. It should appear as ^M, depending on the application you have running. For example, when testing a proxy server I was working on, I used nc (netcat) to connect to it like so:

% nc 127.0.0.1 8080
GET /index.html HTTP/1.1^M
^M
To put the CRLF's on the end of each header, as required by the HTTP protocol, press Control-V, Return, Return. Of course, the ^M is not sent literally -- all the server will see is a CRLF. Too easy!
    •    
  • Currently 2.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[30,447 views]  

Insert carriage return, line feed into Terminal | 4 comments | Create New Account
Click here to return to the 'Insert carriage return, line feed into Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Insert carriage return, line feed into Terminal
Authored by: LC on Jan 21, '04 02:02:33PM

I've typed into HTTP servers (via telnet) for years from Unix using only linefeeds (no <CR>'s). I just tried it again
using OS X's Terminal (to an internet site), and linefeeds (<CTRL>-J) work fine. Is the <CR> requirement you
mentioned particular to certain flavors of HTTP server, or could we be seeing a terminfo/termcap issue? Larry.



[ Reply to This | # ]
Insert carriage return, line feed into Terminal
Authored by: tim1724 on Jan 21, '04 03:43:27PM

The HTTP specification requires the CR/LF combo at the end of each line. Most web servers will allow just LF for compatibility reasons, but some won't. I've seen some which allow just LF if using HTTP/1.0, but which require CR/LF when using HTTP/1.1

Although you might be able to get away with just a linefeed on many servers, the author of this article is correct in saying that HTTP requires the carriage return.

---
Tim Buchheim



[ Reply to This | # ]
Insert carriage return, line feed into Terminal
Authored by: LC on Jan 21, '04 05:12:01PM

Yes of course; sorry, I'd forgotten about the requirement for CRLF in HTTP's control structures. Duh!
(We get to normalize line endings in XML, yay;)



[ Reply to This | # ]
Insert carriage return, line feed into Terminal
Authored by: RideMan on Jan 23, '04 11:38:46PM

Is it just a meaningless coincidence that terminal.app uses the same escape character for "insert, don't interpret next character" (^V) as Apple Writer //e?

(which is still my favorite text editor, but that's beside the point...)



[ Reply to This | # ]