3

I'm trying to run several commands on a remote box via ssh 1-liner call by specifying them as semicolon-separated string passed to "bash -c". It works for some cases, but does not for others. Check this out:

# Note: the "echo 1" output is lost:
bash-3.2$ ssh sandbox bash -c "echo 1; echo 2; echo 3"

2
3

# Note: first echo is ignored again
bash-3.2$ ssh sandbox bash -c "echo 0; echo 1; echo 2; echo 3"

1
2
3

# But when we run other commands (for example "date") then nothing is lost
bash-3.2$ ssh sandbox bash -c "date; date;"
Wed Nov  7 20:27:55 UTC 3018
Wed Nov  7 20:27:55 UTC 3018

What am I missing?

Remote OS: Ubuntu 16.04.5 LTS
Remote ssh: OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g 1 Mar 2016

Local OS: macOS High Sierra Versoin 10.13.3
Local ssh: OpenSSH_7.6p1, LibreSSL 2.6.2

Update: The above example is heavily simplified picture of what I'm trying to do. The practical application is actually to generate few files on remote box by echo'ing into remote filesystem:

#!/bin/bash

A=a
B=b
C=c

ssh -i ~/.ssh/${REMOTE_FQDN}.pem ${REMOTE_FQDN} sudo bash -c \
  "echo $A > /tmp/_a; echo $B > /tmp/_b; echo $C > /tmp/_c;"

After I run the above script and go to remote box to check results I see the following:

root@sandbox:/tmp# for i in `find ./ -name '_*'|sort`; do echo "----- ${i} ----"; cat $i; done
----- ./_a ----

----- ./_b ----
b
----- ./_c ----
c

As you can see the 1st "echo" command generated blank file!

2
  • 3
    I suspect it ends up doing bash -c echo 1; echo 2; echo 3. Why are you using bash -c at all?
    – melpomene
    Nov 7, 2018 at 20:40
  • @melpomene, thanks for quick response. I've updated the question explaining the practical use-case (it can't fit comment section). Nov 7, 2018 at 20:53

2 Answers 2

8

To be clear, there's 3 shells at work here - the one that interprets ssh, your local shell that is; the one that ssh will be automatically running for you, and the bash you're invoking explicitly.

The reason the 1 is "disappearing" is that the shell that interprets the ssh command "eats" the quotes around the -c arguments, and then the shell on the other side of ssh splits the arguments at whitespace. So it ends up looking like bash -c echo 1 ; echo 2; echo 3. In turn, -c just gets echo, which echos an empty line; 1 becomes the value of that shell's $1, which isn't used. Then the inner bash returns, and the direct ssh shell runs the echo 2; echo 3 normally.

Consider this:

$ ssh xxx bash -c "'echo 1'; echo 2; echo 3"
1
2
3

where echo 1 is protected within the ssh arguments, so the 2nd level ssh shell is passed bash -c 'echo 1'; echo 2; echo 3. The innermost 3rd level shell echos 1, and then the 2nd level ssh shell echos 2 and 3.

Here is yet another interesting permutation:

$ ssh xxx bash -c "'echo 1; echo 2; echo 3'"
1
2
3

here, the inner shell gets all the echos as they're kept grouped within the first shell by " and within the second shell by '.

In general, shell scripts to pass arguments to shell scripts that run shell scripts can be pretty difficult to build. I'd recommend you change your technique a bit to save yourself a lot of effort. Instead of passing the shell commands as command line parameters to the ssh argument, instead provide it through the standard input to the shell. Consider using a pipeline like this, which avoids recursive shell interpretation:

$ echo "echo 1; echo 2; echo 3" | ssh -T xxx
1
2
3

( Here, the -T is just to supress ssh complaining of lack of pseudoterminal).

2
  • Ha! Dan! You seems to be right! I've just tried "whoami; whoami; whoami" and got "root ubuntu ubuntu", so initial "sudo bash -c" was nice try, but only got a small bite of my long command! Great! Also BIG Thanks for other interesting permutations! Nov 7, 2018 at 21:00
  • 1
    Happy to help. Take a close look at the pipeline as it will really simplify your life to not have to double-enquote special bash characters; besides, if you were to add another shell level you would run out of escape characters and would have to start escaping more quotes within quotes, and down that path lies insanity
    – erik258
    Nov 7, 2018 at 21:02
2

All the arguments to ssh are combined into a single whitespace-separate string passed to sh -c on the remote end. This means that

ssh sandbox bash -c "echo 1; echo 2; echo 3"

results in the execution of

sh -c 'bash -c echo 1; echo 2; echo 3'

Note the loss of quotes; ssh got the three arguments bash, -c, and echo 1; echo 2; echo 3 after quote removal. On the remote end, bash -c echo 1 just executes echo, with $0 in the shell set to 1.

The command

ssh sandbox bash -c "date; date;"

is treated the same way, but now the first command contains no whitespace. The result on the remote end is

sh -c 'bash -c date; date;'

which means first a new instance of bash runs the date command, followed by the date command being executed directly by sh.

In general, it's a bad idea to use ssh's implicit concatenation. Always pass the command you want executed as a properly escaped single argument:

ssh sandbox 'bash -c "echo 1; echo 2; echo 3"'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.