0

My boss came to me with a question about how to embed a CRLF sequence into his shell script (for piping through some sort of netcat or telnet stuff he's doing) in a reasonably portable and robust way.

Jim Dennis
  • 17,054
  • 13
  • 68
  • 116

2 Answers2

5

Assuming POSIX is a good baseline for "portable enough",

printf "\r\n"
tripleee
  • 175,061
  • 34
  • 275
  • 318
1

My suggestion for him:

#!/bin/sh
crlf="$(echo xy | tr xy '\r\n')"

... but I'm open to other comments. It's certainly fine with modern versions of GNU tr and bash.

Jim Dennis
  • 17,054
  • 13
  • 68
  • 116
  • (I'm tossing this out in the spirit of Jeff Atwood's recent posting: http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Jim Dennis May 23 '12 at 21:25
  • My mistake, need the double quotes around $(...) to preserve that (but worked in my test case and in his usage because the echo command was adding the \n back). – Jim Dennis May 24 '12 at 18:53