i'm trying to print a multi line strings in printf
this way
printf "hi\n"
printf "next line here\n"
I can't do the following
text_content="
hi
next line here
"
printf $text_content
Is there any other way?
Use printf to Make Multi-Line String in Bash We can use printf with the new line character and redirect the output to a file using > . The content in the file does not have extra spaces.
To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.
Printing Newline in Bash The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.
Quoting the variable should do the trick. In your example, however, you are getting double newlines.
printf "$text_content"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With