1

A very simple question. I'd like to write several lines to a file with just one line containing multiple \n. I.e. my input string would be hello\nhello\nhello, I'd like to write it to a file hello.txt, and when cat hello.txt it should give me

hello
hello
hello

However, when I do echo "hello\nhello\nhello" > hello.txt, it just gave me the literal string, without converting \n to a newline.

I wonder how to write such a line to a file.

IsaIkari
  • 1,002
  • 16
  • 31
  • 1
    Did you try `printf` instead of `echo`? [Echo newline in Bash prints literal \n](https://stackoverflow.com/questions/8467424/echo-newline-in-bash-prints-literal-n) – Dominique Dec 14 '21 at 14:38

1 Answers1

0

You want either echo -e or, generally even better printf

printf 'Hello %s\n\nWelcome to my %s document\nmultiline document\n\n\n\nEnd\n' \
       "John" "yellow" > "/some/path/hello.txt";
Raxi
  • 2,452
  • 1
  • 6
  • 10