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 ?
Use the sys. stdin. readlines() method to read user input until EOF. The readlines() method will return a list containing the lines.
This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.
This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended.
Show activity on this post. There is no way to echo out an EOF. An EOF can only be generated either by reaching the end of a file or by invoking the keypress bound to the eof terminal setting ( Ctrl D by default) when the file being read is bound to the terminal.
I would stick to the one-liner
input=$(cat)
Of course, Ctrl-D
should be used to signal end-of-file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With