Script and output is as below :
Script :
#!/bin/bash
#tee_with_read.sh
function tee_test()
{
echo "***This should be printed first but it is not***"
read -r -p "Enter input : "
echo "You entered : $REPLY"
}
tee_test | tee -a logfile
Output :
$ ./tee_with_read.sh
Enter input : ***This should be printed first, but it is not***
"My Input"
You entered : "My Input"
I'm trying to append output to logfile. But as you can see in ouput, it seems like first read gets excuted and then echo which is not as expected.
I'm using Git Bash Version 3.1.23 on windows 10. Since named pipe is not available in this version, I cannot use named pipe for logging purpose.
read -p
is writing it's output to stderr
and echo
is writing to stdout
. stdout
is typically buffered while stderr
is not, and so it's not uncommon to see stderr
things show up before stdout
.
You could have your echo
also go to stderr
if you like by doing echo "string" >&2
or you could run it in the unbuffer
command or similar tools if you have them available
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