Printing a newline

44 views Asked by At

GAAAAAAAAAAAAAAH I don't know why I'm struggling so much with this.

All I want to do is make:

    file="$a"[command-here]"$b"

to make $file=

    a [newline]
    b

And yet no matter WHAT echo or printf I try to use, I cannot get it to work!! I just want a newline between a and b. Can someone bring me out of my misery?

file="$a""echo -e '\n'""$b" does not work, nor can any combination I can think of.

2

There are 2 answers

2
anubhava On BEST ANSWER

You can use:

a='foo'
b='bar'
file="$a"$'\n'"$b"
echo "$file"
foo
bar

Or use print -v:

unset file
printf -v file "$a\n$b"
echo "$file"
foo
bar
0
Kenster On

sh and similar shells preserve newlines in quoted strings. You could do this:

file="$a
$b"

for example:

$ a=foo
$ b=bar
$ file="$a
> $b"
$ echo "$file"
foo
bar