Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I redirect stdin directly to file?

Tags:

bash

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

cat >some_file
like image 867
antak Avatar asked Mar 10 '23 22:03

antak


1 Answers

Use process-substitution in bash, to avoid useless-use-of-cat

> file
while IFS= read -r line
do
  printf "%s\n" "$line" 
done < /dev/stdin >> file

To read form stdin and append to the file on the run, > file truncates the contents of the file before reading starts.

Including this suggestion from Leon's comment for a one-line option to do this,

while IFS= read -r line; do echo "$line"; done > file
like image 177
Inian Avatar answered Mar 21 '23 01:03

Inian