Redirecting output of bash for loop

LinuxBashShell

Linux Problem Overview


I have a simple BASH command that looks like

for i in `seq 2`; do echo $i; done; > out.dat

When this runs the output of seq 2 is output to the terminal and nothing is output to the data file (out.dat)

I am expecting standard out to be redirected to out.dat like it does simply running the command seq 2 > out.dat

Linux Solutions


Solution 1 - Linux

Remove your semicolon.

for i in `seq 2`; do echo "$i"; done > out.dat

SUGGESTIONS

Also as suggested by Fredrik Pihl, try not to use external binaries when they are not needed, or at least when practically not:

for i in {1..2}; do echo "$i"; done > out.dat
for ((i = 1; i <= 2; ++i )); do echo "$i"; done > out.dat
for i in 1 2; do echo "$i"; done > out.dat

Also, be careful of outputs in words that may cause pathname expansion.

for a in $(echo '*'); do echo "$a"; done

Would show your files instead of just a literal *.

$() is also recommended as a clearer syntax for command substitution in Bash and POSIX shells than backticks (`), and it supports nesting.

The cleaner solutions as well for reading output to variables are

while read var; do
    ...   
done < <(do something)

And

read ... < <(do something)  ## Could be done on a loop or with readarray.

for a in "${array[@]}"; do
    :
done

Using printf can also be an easier alternative with respect to the intended function:

printf '%s\n' {1..2} > out.dat

Solution 2 - Linux

Another possibility, for the sake of completeness: You can move the output inside the loop, using >> to append to the file, if it exists.

for i in `seq 2`; do echo $i >> out.dat; done;

Which one is better certainly depends on the use case. Writing the file in one go is certainly better than appending to it a thousand times. Also, if the loop contains multiple echo statements, all of which shall go to the file, doing done > out.dat is probably more readable and easier to maintain. The advantage of this solution, of course, is that it gives more flexibility.

Solution 3 - Linux

Try:

(for i in `seq 2`; do echo $i; done;) > out.dat

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthew KirkleyView Question on Stackoverflow
Solution 1 - LinuxkonsoleboxView Answer on Stackoverflow
Solution 2 - Linuxtobias_kView Answer on Stackoverflow
Solution 3 - LinuxsvanteView Answer on Stackoverflow