Suppose you want to have a program that ingest a string and then reattach stdin when done.
I know that it is possible to do so by doing this trick:
(echo id; cat) | sh
Which works well. It doesn't close stdin since it is used by cat. But the output is messy.
Digging deeper I understood that it is lacking of a tty.
If my comprehension is right, sh being in a pipe didn't open a tty.
The trick I've found is to use expect.
Simply put this in a file:
spawn sh
reattach
Then when cat gives you back the control, do:
expect exp.sh
This is nice because it gives me a tty to do everything I need: ssh, tmux and so on...
The only thing I don't understand why is that I see everything I typed, back to my terminal.
Example:
(echo whoami; cat) | sh
moon
expect exp.sh
spawn sh
sh-3.2$ whoami
whoami
moon
Note the whoami output back before my login on the last line.
Could somebody explain to me why? It is obviously not a property of stdout from cat because it is attached to sh directly -- yes, even if it was dumb I tried the following: (echo id; cat > /dev/null) | sh then nothing happened.
So is it a property of a tty/pty to not display back what has been typed from the keyboard?