linux - Redirecting output of bash for loop -
i have simple bash command looks like
for in `seq 2`; echo $i; done; > out.dat
when runs output of seq 2
output terminal , nothing output data file (out.dat)
i expecting standard out redirected out.dat running command seq 2 > out.dat
remove semicolon.
for in `seq 2`; echo "$i"; done > out.dat
suggestions
also suggested fredrik pihl, try not use external binaries when not needed, or @ least when practically not:
for in {1..2}; echo "$i"; done > out.dat ((i = 1; <= 2; ++i )); echo "$i"; done > out.dat in 1 2; echo "$i"; done > out.dat
also, careful of outputs in words
may cause pathname expansion.
for in $(echo '*'); echo "$a"; done
would show files instead of literal *
.
$()
recommended clearer syntax command substitution in bash , posix shells backticks (`
), , supports nesting.
the cleaner solutions reading output variables are
while read var; ... done < <(do something)
and
read ... < <(do something) ## done on loop or readarray. in "${array[@]}"; : done
using printf can easier alternative respect intended function:
printf '%s\n' {1..2} > out.dat
Comments
Post a Comment