I am trying to run a few commands through ssh
and getting confused with the behavior of sh -c
:
ssh myhost sh -c 'echo starting; who -b; date; echo $SHELL'
Output (note: the echo's output is just a blank line!)
system boot 2016-12-22 20:22
Thu Jan 26 06:12:52 UTC 2017
/bin/bash
Without sh -c
I get the correct output:
ssh myhost 'echo starting; who -b; date; echo $SHELL'
Output:
starting
system boot 2016-12-22 20:22
Thu Jan 26 06:18:28 UTC 2017
/bin/bash
Questions:
- Why doesn't
sh -c
handle theecho starting
command correctly? It outputs a blank line. - Why is SHELL set to
/bin/bash
even withsh -c
?
$SHELL
would be expanded by the login shell of the remote user as it's between double quotes (and assuming that shell is Bourne-like or csh-like orfish
). You'd need to escape the$
if you wanted the$SHELL
to be expanded bysh
:ssh myhost 'sh -c "echo starting; who -b ; date; echo \$SHELL"'
– Stéphane Chazelas Jan 26 '17 at 12:27