Run the export command over SSH

When I run the bash shell from the following:

 bash -c '(export abc=123 && echo $abc)' 

Output: "123". But when I run it on top of ssh :

 ssh remote-host "bash -c '(export abc=123 && echo $abc)'" 

There is no conclusion. Why is this? Is there any way around this? That is, is there a way to set the environment variable for the command over which I am running ssh ?

Note. When I replace echo $abc with something standard, like echo $USER , the ssh command prints the username on the remote computer as expected, as it is already installed.

I am running RHEL 5 Linux with OpenSSH 4.3

+4
source share
1 answer

This is due to the fact that when using

 ssh remote-host "bash -c '(export abc=123 && echo $abc)'" 

the variable is expanded by the local shell (as is the case with $USER ) until ssh executed. Escape $ using \$ and it should do subtle

 ssh remote-host "bash -c '(export abc=123 && echo \$abc)'" 

On a side note:

  • You do not need to export just for this.
  • You do not need to wrap it ()

Same:

 ssh remote-host "bash -c 'abc=123 && echo \$abc'" 

Hell, you can even leave bash -c ... stuff as ssh manpage says:

If a command is given, it runs on the remote host instead of the login shell.

But it may be specific to your task;)

+11
source

Source: https://habr.com/ru/post/1335173/


All Articles