Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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 have a very simple ksh script and at certain points I want to write to a log file. I use the following commands in two places...

print "Directory listing 1:\n" > ${LogFile}
ll >> ${LogFile}

(Note: The second time this command is used print Directory listing 2)

My problem is, when I view the log file afterwards, only the second execution of these commands work! So there's no "Directory listing 1" and accompanying "ll" output.

I have tested and tested the script to ensure that there's nothing wrong my logic. I've added print test commands just before each so I know they get executed.

Is there something I've done wrong or I'm not realising?

share|improve this question
Not a bug. You're telling it to start LogFile from scratch, no wonder it has no "Directory listing 1". – njsg Jan 25 '13 at 12:59
up vote 3 down vote accepted

Whenever you do a redirection with > (your first line), the ${LogFile} is truncated to 0 and then written. If I understand right, you do the above twice, the first stuff is overwritten by the second.

What you have to do is along the lines:

> ${LogFile}    # This just truncates if there was anything there, writes nothing
echo "First round" >> ${LogFile}
ls -l >> ${LogFile}
echo -e "\nSecond round" >> ${LogFile}
ls -l >> ${LogFile}
share|improve this answer
That's exactly what my issue was - thanks for your help! – Rich Jan 25 '13 at 13:24

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.