EDIT: I realized that
tee can read from the original
stdin if nothing is piped to it, so I thought I'd add another solution in case it helps anyone who stumbles upon this later.
Thanks to @steeldriver for pointing out that
read -p prints to
stdout. So closest I've got is:
read -p " - This directory is not empty, purge? (Y/N): " choice 2> >(tee -a $mylogs)
echo $choice >> $mylogs
Source and explanation for what's going on can be found here. For this specific example, the first line is able to redirect
tee to print the prompt in
$mylogs and to
read seems to be doing something to make it impossible to intercept the user input and send it to
$mylogs and not just
The other solution is simpler and shorter, but less user friendly and readable (unless many people know that
read isn't the only command to read input so simply):
echo -n " - This directory is not empty, purge? (Y/N): " | tee -a $mylogs
choice=$(tee -a $mylogs)
So in the second line
tee provides a way to give the user input to both
read, but obviously there's no prompt option (
-p). So still 2 lines.
Also, you have to press Enter then Ctrl+D to submit. This might be bad for my Y/N situation, but might be nice for multi-line answers since you can press Enter multiple times before pressing Ctrl+D to stop.