How can I do variable substitution in this putty plink echo command

286 Views Asked by At

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?

2

There are 2 best solutions below

0
Randall On BEST ANSWER

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

4
Randall On

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