Output for loop to a file
You're only redirecting the echo with ">", so it overwrites. What you need is to append using ">>" operator. Do the following:
for ((i=1;i<=10;i++)) ; do echo "Hello World" >> testforloop.txt ; done
You are using >
redirection, which wipes out the existing contents of the file and replaces it with the command's output, inside the loop. So this wipes out the previous contents 10 times, replacing it with one line each time.
Without the >
, or with >/dev/tty
, it goes to your display, where >
cannot wipe anything out so you see all ten copies.
You could use >>
, which will still open the file ten times, but will append (not wipe out previous contents) each time. That's not terribly efficient though, and it retains data from a previous run (which may or may not be what you want).
Or, you can redirect the entire loop once:
for ... do cmd; done >file
which runs the entire loop with output redirected, creating (or, with >>
, opening for append) only once.