-1

This is driving me nuts.

nl=$"\n"

message="bla bla $some_var such $another_var ${nl} wow $another_var"
echo -e "$message" > file.txt

Just prints:

bla bla some_var such another_var wow another_var

I want it to print:

bla bla some_var such another_var
wow another_var

SOLUTION: don't use \n when outputting to html file use <br/> instead LOL

16
  • 1
    Aside from the missing closing quote in the variable assignment, the above code should work well.
    – devnull
    Commented Dec 11, 2013 at 9:05
  • Oh yeah fixed. I'll edit the first post to show my actual script. Commented Dec 11, 2013 at 9:09
  • echo tends to differ a bit depending on version/shell. show echo --version. You can also switch to printf which should work regardless. Commented Dec 11, 2013 at 9:16
  • Using printf it just prints everything on one line also. printf "Starbound server status:${nl}${nl}$online ${nl} It is using $real_cpu CPU and $real_memory MB out of 16GB of memory." > status.htm. How do I echo --version without printing --version? :( Commented Dec 11, 2013 at 9:24
  • Doing printf "Cake \n pie" > file.txt doesn't even work. Commented Dec 11, 2013 at 9:27

2 Answers 2

2

Your comment on your original question makes me believe you are printing HTML to a file. How are you checking whether it has newlines? If you want to get actual newlines on your website, you should use <br> instead of \n.

0
2

$"bla" is used for translations using the current locale in bash. What you want is the $'bla' notation which knows of these backslash escapes.

Not the answer you're looking for? Browse other questions tagged or ask your own question.