33

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"
2
  • 1
    Do you NEED to create this "logwrite" variable? Can you write printf "%s\n" $var1 $var2 >> file? Jul 22, 2011 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, 2011 at 22:45

2 Answers 2

47
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
5
  • 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, 2011 at 20:32
  • 1
    @dave it appears you are using the wrong slash with the escape sequence. \n instead of /n should do the trick :)
    – user1931
    Jul 22, 2011 at 20:34
  • That fixes it (using \n instead of /n. Thanks so much!!
    – daviesgeek
    Jul 22, 2011 at 20:38
  • @dave you're more than welcome :)
    – user1931
    Jul 22, 2011 at 20:39
  • on AIX in KSH script I did not require the -e flag. Jun 17, 2015 at 16:03
5
var1='Hello'
var2='World!'
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
Hello
World
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"

3
  • 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 :)
    – user1931
    Jul 22, 2011 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, 2011 at 20:41
  • Yes, use printf. It's POSIX: stackoverflow.com/a/34947759/895245 Jan 23, 2016 at 23:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .