I have two scripts:
foo.sh:
#!/bin/bash
echo -e "one\ntwo" |
while read line; do
cat not-existing
echo hello $line
done
bar.sh:
#!/bin/bash
echo -e "one\ntwo" |
while read line; do
ssh user@machine 'cat not-existing' # Here is the only difference
echo hello $line
done
And now I run them
$ ./foo.sh
cat: not-existing: No such file or directory
hello one
cat: not-existing: No such file or directory
hello two
$ ./bar.sh
cat: not-existing: No such file or directory
hello one
The output of bar.sh
is surprising to me. I would expect it to be the same for both scripts.
Why does the output of foo.sh
and bar.sh
differ? Is it a bug or a feature?
Note
The following works as I expect, i.e. the output of this is the same as the output of foo.sh
:
#!/bin/bash
for line in `echo -e "one\ntwo"`; do
ssh user@machine 'cat not-existing'
echo hello $line
done
Why?