I’m working on a bash script that will ssh to a remote machine and execute a series of commands there. I’m new to ssh. I’m trying several ways on how to do this the cleanest way possible. I saw on this website that one option is to pass a heredoc (“here document”) to the ssh command. I also want to use the bash option set –x
for debugging.
I’m trying several things to see what my options are. I do not understand why the following behavior happens. If I run:
ssh remote_machine bash -x <<- REMOTE
echo pear
echo apple
REMOTE
the following is shown:
pear
apple
+ echo pear
+ echo apple
As you can see, the output is not in order. But if I do locally:
bash -x <<- REMOTE
echo pear
echo apple
REMOTE
the following is shown:
+ echo pear
pear
+ echo apple
apple
as it should be.
I tried to find the answer online but couldn’t find anything. Any ideas?
E D I T 1: According to @user1934428,
The output of echo goes to stdout, and the output of -x goes to stderr, and ssh has to catch somehow the two streams and can't know how to sync them.
I searched online if it's possible to "merge" stderr and stdout for multiple commands. This is possible, with line exec 2>&1
. This redirects the stderr to stdout for succeeding commands. See more here: sh command: exec 2>&1
So now I can do:
ssh remote_machine bash <<- REMOTE
exec 2>&1
set -x
echo pear
echo apple
echo peach
echo plum
REMOTE
which yields:
+ echo pear
pear
+ echo apple
apple
+ echo peach
peach
+ echo plum
plum
I'll need some time to consider the implications of this and to decide if I'm OK with this behavior.