Read full stdin before EOF when stdin comes from `cat` bash

I am trying to read the full stdin in a variable:

script.sh

#/bin/bash

input=""
while read line
do
  echo "$line"
  input="$input""\n""$line"
done < /dev/stdin



echo "$input" > /tmp/test

When I run, ls | ./script.shor basically any other commands, it works fine.

However, it does not work, when I start cat | ./script.sh, enter my message, and then press Ctrl-C to exit cat.

Any ideas?

+4
source share
1 answer

I would stick with a single line

input=$(cat)

Of course, Ctrl-Dshould be used to mark the end of the file.

+5
source

All Articles