Sam12
Sam12

Reputation: 1805

How concatenate strings with a newline

I want to string concatenation two given strings line1 and line2 and entering a newline between them. Any idea please?

I tried the following but it didn't work:

enter='\n'
lines=$line1$enter$line2

Upvotes: 5

Views: 5821

Answers (1)

John Kugelman
John Kugelman

Reputation: 361556

Use $'...' to have the shell interpret escape sequences.

enter=$'\n'
lines=$line1$enter$line2

You can also put a newline directly inside double quotes:

lines="$line1
$line2"

Or use printf:

printf -v lines '%s\n%s' "$line1" "$line2"

Upvotes: 12

Related Questions