edi9999
edi9999

Reputation: 20554

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

I'm trying to read full stdin into 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.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

Answers (1)

Roberto Reale
Roberto Reale

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

Related Questions