Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How concatenate strings with a newline

Tags:

bash

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
like image 846
Sam12 Avatar asked Apr 13 '18 14:04

Sam12


1 Answers

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"
like image 200
John Kugelman Avatar answered Oct 12 '22 23:10

John Kugelman