Execute bash command with calling subshell $() via ssh with Here Document [duplicate]
Asked Answered
F

2

5

I try to send command via ssh which looks like this:

ssh [email protected] "echo $(uname -a)"

But my problem is, that $(uname -a) part actually create a subshell and executes not on 192.168.1.1 server, but on my system, from which I executed this command.

How can I fix it?

P.S. My actual example involved docker stop all command, which looks like

docker stop $(docker ps -q)

but I simplified question.

UPDATED: Sorry, I oversimplify my question. My command use Here Document (because inside command is complex and use a lot of different quotes marks)

ssh [email protected] <<SSHCOMMAND
  echo $(uname -a)
SSHCOMMAND

So Ignacio Vazquez-Abrams solution will not work

Fu answered 11/9, 2015 at 12:19 Comment(0)
F
7

I found solution -

ssh [email protected] <<'SSHCOMMAND'
  echo $(uname -a)
SSHCOMMAND

Pretty strange for me, but works, thanks Ignacio Vazquez-Abrams for correct name of this process 'substitution', I couldn't find before :)

Fu answered 11/9, 2015 at 12:42 Comment(1)
See 'man sh' near the bottom of the section 'Redirections'. It is all documented there.Jolo
A
7

Single quotes suppress all substitution.

ssh [email protected] 'echo $(uname -a)'
Anaya answered 11/9, 2015 at 12:25 Comment(1)
Thank you, your solution works fine, but I added more complex example, which is closer to my actual command.Fu
F
7

I found solution -

ssh [email protected] <<'SSHCOMMAND'
  echo $(uname -a)
SSHCOMMAND

Pretty strange for me, but works, thanks Ignacio Vazquez-Abrams for correct name of this process 'substitution', I couldn't find before :)

Fu answered 11/9, 2015 at 12:42 Comment(1)
See 'man sh' near the bottom of the section 'Redirections'. It is all documented there.Jolo

© 2022 - 2024 — McMap. All rights reserved.