70

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

Matthew Kirkley
  • 4,138
  • 5
  • 31
  • 33

3 Answers3

103

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
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • 1
    Why `seq` instead of `for i in {1..2}; do ...`? – Fredrik Pihl Sep 04 '13 at 12:53
  • @FredrikPihl Yes it's better. Or `(( i = 1 ; i <= 2; ++i ))`, or simply `in 1 2` for compatibility. My answer is only about the problem with redirection, although I tried to fix one: `"$i"`. – konsolebox Sep 04 '13 at 12:56
  • I just thought you could update your answer with an alternative *better* way to solve the problem without resorting to using external binaries :-) – Fredrik Pihl Sep 04 '13 at 12:58
  • @FredrikPihl Well it's not really connected to the problem and is more of a suggestion instead, but I made an update anyway. – konsolebox Sep 04 '13 at 13:16
  • 5
    This example led me to realize that I can pipe the `done` statement to other things like `sort`, for example `for i in foo bar baz; do echo $i | base64; done | sort | base64 -d` – ThorSummoner Oct 16 '14 at 23:30
  • If it's really only going to be a small static straight sequence such as {1..2}, you might as well break it out into 1 2, as konsolebox said. E.g. `for a in 1 2; ...`, `printf '%s\n' 1 2`. –  May 11 '16 at 14:44
9

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.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
6

Try:

(for i in `seq 2`; do echo $i; done;) > out.dat
svante
  • 1,375
  • 8
  • 9
  • That worked. Is there a resource where I can read up on what the parenthesis do/why it is required. Or could you explain? – Matthew Kirkley Sep 04 '13 at 11:30
  • 4
    It's not necessary to place it on a subshell. The semicolon just have to be removed to make the redirection part of the block. – konsolebox Sep 04 '13 at 11:31
  • 2
    True, just removing the semicolon will do in this case. Subshell, ( ), can be used to collect output from larger number of commands, say multiple for-loops. – svante Sep 04 '13 at 12:31
  • 1
    Unable to find a good subshell article on stackoverflow. Here is one external: http://tldp.org/LDP/abs/html/subshells.html – svante Sep 04 '13 at 12:31
  • 2
    @svante I agree with enclosing it around for more commands, but you could also do that with `{}` when it's not necessary to create another process for the subshell and keeping the variable changes from the higher block. A situation only when it can't be helped is when you place them on a pipeline since they are placed on a subshell by default, where not laspipe is set and commands are on the end, although using process substitutions are the way to go for it. – konsolebox Sep 04 '13 at 12:47
  • 3
    The `for` loop is already a compound command that groups its body; the subshell here is pointless. – chepner Sep 04 '13 at 13:02
  • @konsolebox, excellent points! My own scripts could probably do with a lot more `{}`'s :) – svante Sep 04 '13 at 13:19