I’m generating a script like this
(echo "ssh hans@mymachine.com echo hello" ; echo 'echo Done')
which produces
ssh hans@mymachine.com echo hello
echo Done
If I store the generated script in a file
(echo "ssh hans@mymachine.com echo hello" ; echo 'echo Done') > a.sh
and execute it
. ./a.sh
then it works oke and produces
Warning: No xauth data; using fake authentication data for X11 forwarding.
hello
Done
If I’m executing the script by piping these commands to bash
(echo "ssh hans@mymachine.com echo hello" ; echo 'echo Done') | bash
The result is
Warning: No xauth data; using fake authentication data for X11 forwarding.
hello
As you can see bash did not execute the ‘echo Done’ command.
My first thought was that ssh probably produces a non zero return value probaby caused by this xauth warning. So I did this
ssh hans@mymachine.com echo hello
echo $?
which produced
Warning: No xauth data; using fake authentication data for X11 forwarding.
hello
0
So return value is oke. Now I tried using a trap
trap "" ERR
but this also didn’t help. Pretty logical as the return value was zero anyway.
So I’m lost. Why is the ‘echo Done’ command not being executed?
UPDATE:
I noticed that when I wrap the script with brackets it works. So
( echo "(" ; echo "ssh hans@mymachine.com echo hello" ; echo 'echo Done'; echo ")" ) | bash
produces
Warning: No xauth data; using fake authentication data for X11 forwarding.
hello
Done
sshis consuming stdin, which in this case is theecho. Add-n.