Shell script echo new line to file

echoshellshell-script

I want to echo a new line to a file in between variables in a shell script. Here's my code:

var1="Hello"
var2="World!"
logwrite="$var1 [Here's where I want to insert a new line] $var2
echo "$logwrite"  >> /Users/username/Desktop/user.txt

Right now, when I run my script, the file user.txt shows this:

Hello World!

I want it to show:

Hello
World!

How do I do this??

EDIT: Here's my shell script:

echo -n "What is your first name? "
read first
echo -n "What is your last name? "
read last
echo -n "What is your middle name? "
read middle
echo -n "What is your birthday? "
read birthday
echo -e "First Name: $first /nLast Name: $last /nMiddle Name: $middle /nBirthday: $birthday" >> /Users/matthewdavies/Desktop/user.txt
qlmanage -p "~/Desktop/user.txt"

Best Answer

var1="Hello"
var2="World!"
logwrite="$var1\n$var2"
echo -e "$logwrite"  >> /Users/username/Desktop/user.txt

Explanation:

The \n escape sequence indicates a line feed. Passing the -e argument to echo enables interpretation of escape sequences.

It may even be simplified further:

var1="Hello"
var2="World!"
echo -e "$var1\n$var2"  >> /Users/username/Desktop/user.txt

or even:

echo -e "Hello\nWorld! "  >> /Users/username/Desktop/user.txt
Related Question