I need to define the "\n
" in END block
as it will be passed to other bash script as an argument.
I have added an empty newline
at the begin
and at the end
. But only the begin empty line (\n)
is working and the end empty line (\n)
is not working.
Here is the test script:
string="$(cat <<"END"foothe "newline(\n)" in "END" before the foo is workingbut "newline(\n)" in "END" after the bar is not workingbarEND)"echo ""echo "--- method 1 - define \n in END block ---"echo ""printf "%s" "first line in single line (method 1)"printf "%s" "$string"printf "%s" "last line in single line (method 1)"echo ""echo ""echo "--- method 2 - define \n in printf ---"echo ""printf "%s" "first line in single line (method 2)"printf "%s\n" "$string"printf "%s" "last line in single line (method 2)"
Here is the output:
--- method 1 - define \n in END block ---first line in single line (method 1)foothe "newline(\n)" in "END" before the foo is workingbut "newline(\n)" in "END" after the bar is not workingbarlast line in single line (method 1)--- method 2 - define \n in printf ---first line in single line (method 2)foothe "newline(\n)" in "END" before the foo is workingbut "newline(\n)" in "END" after the bar is not workingbarlast line in single line (method 2)
Here is the expected output:
--- method 1 - define \n in END block ---first line in single line (method 1)foothe "newline(\n)" in "END" before the foo is workingbut "newline(\n)" in "END" after the bar is not workingbarlast line in single line (method 1)
How to set the "\n
" in the END block
for making it work with method 1
?
Update:
I tried to use variable
inside the method provided by @tripleee
:
var1="hello world"string='foothe "newline(\n)" in "END" before the foo is workingbut "newline(\n)" in "END" after the bar is not working$var1bar'printf "%s" "$string"
Here is the output:
foothe "newline(\n)" in "END" before the foo is workingbut "newline(\n)" in "END" after the bar is not working$var1bar
The $var1
is not replaced by the real content
.
If I switch to the double quotes (")
, then it must be use many escape characters (\")
inside the "long long long and complex...
" content.
How to make $var1
working with this method (without END block
)?