Can I redirect stdin directly to a file?

In bash, can I somehow get rid of the part cat(and fork that it brings) on the next command line?

cat >some_file
+4
source share
2 answers

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
+5
source

To use only bash tools, you can:

printf '%s'  "$(< infile)"

But both:

  • I do not believe that it will be much faster for small files.
  • , .

cat .

+2

Source: https://habr.com/ru/post/1669117/


All Articles