@14moose: No actually he has the syntax correct. the
$'string' structure is designed for expanding backslashed escapes into their ascii equivalents. Just quoting them doesn't do that. The bash man page section on QUOTING details it.
I don't see anything wrong with the script as written, and I don't get the same problem in testing. Are you perhaps using
#!/bin/sh instead of
#!/bin/bash?
(Edit: I'll bet that's it. Using
#!/bin/sh reproduces the behavior.
$'string' is a bash-specific function that's not available in posix-compliance mode.)
You can always use an actual newline instead of the string substitution, in any case.
BTW, please use [code][/code] tags around your code, to improve readability and to preserve formatting.