Reputation: 20554
I'm trying to read full stdin into a variable :
#/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.sh
or mostly any other commands, it works fine.
However It doesn't work when I run cat | ./script.sh
, enter my message, and then hit Ctrl-C to exit cat.
Any ideas ?
Upvotes: 8
Views: 10196
Reputation: 4317
I would stick to the one-liner
input=$(cat)
Of course, Ctrl-D
should be used to signal end-of-file.
Upvotes: 11