Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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:


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"
share|improve this question
Do you NEED to create this "logwrite" variable? Can you write printf "%s\n" $var1 $var2 >> file? – glenn jackman Jul 22 '11 at 22:22
No, I don't need to and in my revised script that I've been working on, I don't use the $logwrite variable. Thanks. – daviesgeek Jul 22 '11 at 22:45
up vote 10 down vote accepted
echo -e "$logwrite"  >> /Users/username/Desktop/user.txt


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:

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

or even:

echo -e "Hello\nWorld! "  >> /Users/username/Desktop/user.txt
share|improve this answer
Your example works, but when I incorporate it in my code, it doesn't work. I'll edit my post and add my script in it. – daviesgeek Jul 22 '11 at 20:32
@dave it appears you are using the wrong slash with the escape sequence. \n instead of /n should do the trick :) – John T Jul 22 '11 at 20:34
That fixes it (using \n instead of /n. Thanks so much!! – daviesgeek Jul 22 '11 at 20:38
@dave you're more than welcome :) – John T Jul 22 '11 at 20:39
on AIX in KSH script I did not require the -e flag. – javaPlease42 Jun 17 '15 at 16:03
echo "$var1" >> /Users/username/Desktop/user.txt
echo "$var2" >> /Users/username/Desktop/user.txt

or actually you don't need vars:

echo 'Hello' >> /Users/username/Desktop/user.txt
echo 'World!' >> /Users/username/Desktop/user.txt

there is a problem with john t answer: if any of the vars had the \n string (or some other sequence like \t) they will get translated. one can get something similar to his answer with printf:

printf '%s\n%s\n' 'Hello' 'World!'

also hmm. i see you are composing the answer into a variable $logwrite. if this is the sole use of this variable, it seems pointless.

I think a here-doc could be more readable, specially if you have lots of line to append to the log:

cat >> /Users/username/Desktop/user.txt <<EOF

(this word, EOF, is a delimiter you can choose. it can be any word).

beware that the heredoc will expand $variables, like double quotes. if you don't want this, you quote the heredoc, like <<"EOF"

share|improve this answer
the thing is none of the vars have \n, hence why I supplied the answer. The "what if" methodology would carry every question on this site into an endless discussion. When he comes to that road he can ask another question :) – John T Jul 22 '11 at 20:38
@elias the $logwrite variable was leftover from my edits on the script as I was making changes. I took the $logwrite var out of my code. John T.'s answer did actually work. Thanks so much! – daviesgeek Jul 22 '11 at 20:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.