Given the script
$ cat save_stdin.sh
#!/bin/bash
# save stdin
exec 3<&0
echo " intermediate output independing from stdin:"
echo "This is a test " | grep "test" | tr '[:lower:]' '[:upper:]'
# reset stdin
exec 0<&3
echo -e "n work with original stdin:"
ssh localhost grep "piped" | tr '[:lower:]' '[:upper:]'
# close saved stdin
exec 3<&-
I get as expected
$ echo "this is piped" | ./save_stdin.sh
intermediate output independing from stdin:
THIS IS A TEST
work with original stdin:
THIS IS PIPED
But if I insert a command like ssh localhost ps | wc -l
before the reset, it seems to destroy the saved stdin:
#!/bin/bash
# save stdin
exec 3<&0
echo " intermediate output independing from stdin:"
echo "This is a test " | grep "test" | tr '[:lower:]' '[:upper:]'
##
ssh localhost ps | wc -l
##
# reset stdin
exec 0<&3
echo -e "n work with original stdin:"
ssh localhost grep "piped" | tr '[:lower:]' '[:upper:]'
# close saved stdin
exec 3<&-
gives
$ echo "this is piped" | ./save_stdin.sh
intermediate output independing from stdin:
THIS IS A TEST
86
work with original stdin:
How can I circumvent this (bug!?) without creating temporary files?