-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

pmod
  • 10,450
  • 1
  • 37
  • 50
user3079979
  • 105
  • 1
  • 1
  • 7
  • 1
    Aside from the missing closing quote in the variable assignment, the above code should work well. – devnull Dec 11 '13 at 09:05
  • Oh yeah fixed. I'll edit the first post to show my actual script. – user3079979 Dec 11 '13 at 09:09
  • echo tends to differ a bit depending on version/shell. show echo --version. You can also switch to printf which should work regardless. – Reinstate Monica Please Dec 11 '13 at 09: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`? :( – user3079979 Dec 11 '13 at 09:24
  • Doing `printf "Cake \n pie" > file.txt` doesn't even work. – user3079979 Dec 11 '13 at 09:27
  • You can do /full/path/echo --version (generally /bin/echo). That said if printf "Cake \n pie" > file.txt doesn't work, my guess is your're using some bizarre text editor to read the file. Try it without re-directing the output. – Reinstate Monica Please Dec 11 '13 at 09:30
  • It works without outputting to a file! But I need to output it to a file. – user3079979 Dec 11 '13 at 09:44
  • @user3079979 What text editor are you using to read the file? Try `cat`, `gedit`, or `vi` – Reinstate Monica Please Dec 11 '13 at 09:46
  • `msg="nice test\ntoday3"; echo -e "$msg" > test.txt` works for me – RedX Dec 11 '13 at 09:47
  • What are you opening the file in? Since \n is unix newline it will not display correctly in DOS editors such as Notepad. – qstebom Dec 11 '13 at 09:53
  • I'm using nano, why would using a different text editor matter? gedit reports "cannot open display" cat just prints whats inside the bash script file without any editing and I don't understand vi. I don't get why this has to be so hard :/ – user3079979 Dec 11 '13 at 09:53
  • See this http://stackoverflow.com/questions/3569997/view-line-endings-in-a-text-file – qstebom Dec 11 '13 at 09:54
  • @RedX That works for me too. I don't get it what's the difference?! I added the `;` to the end but that didn't change anything. – user3079979 Dec 11 '13 at 09:56
  • Ok I guess it has to be the text editor I'm using? I guess I will figure out how to use vi – user3079979 Dec 11 '13 at 09:59
  • `the_nl=$'\n';msg="nice test${the_nl}today5"; echo -e "$msg" > test.txt; cat test.txt` this also works for me – RedX Dec 11 '13 at 10:00
  • The thing with the text editor is, if you were using notepad.exe for some reason you would not see a line break as it needs '\r\n' and not only a '\n'. But gedit should work with all kinds of line breaks without a problem. If you `cat` the output you should also see a line break if its in there. – RedX Dec 11 '13 at 10:01

2 Answers2

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.

Alfe
  • 56,346
  • 20
  • 107
  • 159
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.