Use process substitution in bashto avoiduseless-use-of-cat
> file
while IFS= read -r line
do
printf "%s\n" "$line"
done < /dev/stdin >> file
To read the form stdinand add to filein the run, > filetruncates the contents of the file before reading.
Including this sentence from Leon's comment for the single line option to do this,
while IFS= read -r line; do echo "$line"; done > file
source
share