Roguebantha
Roguebantha

Reputation: 824

Printing a newline

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.

Upvotes: 1

Views: 40

Answers (2)

Kenster
Kenster

Reputation: 25380

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

Upvotes: 0

anubhava
anubhava

Reputation: 784888

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

Upvotes: 2

Related Questions