2

I'm trying to break down the following plink command and modify it to allow a variable substitution.

plink.exe 192.168.1.1 --% echo """#define MYSPACE""" > /home/my/file.txt

I want to do the following, but But what follows the echo is sent to bash verbatim.

plink.exe 192.168.1.1 --% echo """#define MYSPACE""" > /$(home)/my/file.txt

Constructing the echo string before the plink command doesn't work for the same reason; everything after echo is sent verbatim. How can this be done?

Glen G
  • 71
  • 5

2 Answers2

2

On the PowerShell side, if home is a defined environment variable, that will still be expanded after the --%. So,

plink.exe 192.168.1.1 --% echo """#define MYSPACE""" > /%home%/my/file.txt

For additional information on how the --% works, see "the stop parsing token" at learn.microsoft.com

Randall
  • 2,859
  • 1
  • 21
  • 24
0

For variable expansion on the bash side, your variable substitution syntax is slightly off.

Most shells want the variable name surrounded by curly braces - { }. But it looks like you are using parentheses. In bash, that $(...) construct will try to run a subshell, and you'll get very odd errors.

So, try
plink.exe 192.168.1.1 --% echo """#define MYSPACE""" > /${home}/my/file.txt

Randall
  • 2,859
  • 1
  • 21
  • 24
  • 1
    Clarification - I want the variable substitution to happen in PowerShell before the command is sent. So far I can't get PS to do any subst after the --%. I can subst before the --%; i.e. plink.exe $ip_addr. Whether I use $(var) or ${var}, I get an invalid subst error from bash because it doesn't know what to do with ${var} – Glen G Sep 09 '22 at 16:24
  • 1
    That's the very point of the `--%`. – Martin Prikryl Sep 09 '22 at 16:37
  • Ah, I should have asked what the --% was for; I couldn't find it in a gsearch; I'll remove and try a couple of other things; report back – Glen G Sep 09 '22 at 16:40
  • @GlenG Some Microsoft documentation on `--%` - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7.2#the-stop-parsing-token – Randall Sep 10 '22 at 16:47