Passing variables in a remote ssh command

I want to be able to run a command from my computer using ssh and go through the environment variable $BUILD_NUMBER

Here is what I am trying:

 ssh pvt@192.168.1.133 '~/tools/myScript.pl $BUILD_NUMBER' 

$BUILD_NUMBER set on the machine, $BUILD_NUMBER an ssh call, and since the variable does not exist on the remote host, it is not taken.

How to pass the value of $BUILD_NUMBER ?

+77
linux bash shell ssh
Jul 23 '10 at 1:03
source share
5 answers

If you use

 ssh pvt@192.168.1.133 "~/tools/run_pvt.pl $BUILD_NUMBER" 

instead

 ssh pvt@192.168.1.133 '~/tools/run_pvt.pl $BUILD_NUMBER' 

your shell will interpolate $BUILD_NUMBER before sending the command line to the remote host.

+151
Jul 23 2018-10-10T00:
source share

Variables in single quotes are not evaluated. Use double quotes:

 ssh pvt@192.168.1.133 "~/tools/run_pvt.pl $BUILD_NUMBER" 

The shell will expand variables in double quotes, but not in single quotes. This will change to the desired line before passing it to the ssh command.

+26
Jul 23 2018-10-10T00:
source share

As already mentioned, you do not need to set the environment variable on the remote host. Instead, you can simply perform a meta extension on the local host and pass the value to the remote host.

 ssh pvt@192.168.1.133 '~/tools/run_pvt.pl $BUILD_NUMBER' 

If you really want to set the environment variable on the remote host and use it, you can use the env program

 ssh pvt@192.168.1.133 "env BUILD_NUMBER=$BUILD_NUMBER ~/tools/run_pvt.pl \$BUILD_NUMBER" 

In this case, this is a bit overkill, and note

  • env BUILD_NUMBER=$BUILD_NUMBER is performed on local hosting
  • remote environment variable BUILD_NUMBER will be used with the remote shell
0
Aug 29 '17 at 11:21 on
source share

(This answer may seem overly complicated, but as far as I know, it is easily extensible and reliable in terms of spaces and special characters.)

You can transfer data directly through standard ssh and read command input from a remote location.

In the following example

  1. The indexed array is filled (for convenience) with the names of the variables whose values ​​you want to get on the remote side.
  2. For each of these variables, we give an ssh string with a null character at the end, giving the name and value of the variable.
  3. In the shh command itself, we shh over these lines to initialize the necessary variables.
 # Initialize examples of variables. # The first one even contains whitespace and a newline. readonly FOO=$'apjlljs ailsi \n ajlls\t éjij' readonly BAR=ygnàgyààynygbjrbjrb # Make a list of what you want to pass through SSH. # (The "unset" is just in case someone exported # an associative array with this name.) unset -v VAR_NAMES readonly VAR_NAMES=( FOO BAR ) for name in "${VAR_NAMES[@]}" do printf '%s %s\0' "$name" "${!name}" done | ssh user@somehost.com ' while read -rd '"''"' name value do export "$name"="$value" done # Check printf "FOO = [%q]; BAR = [%q]\n" "$FOO" "$BAR" ' 

Exit:

 FOO = [$'apjlljs ailsi \n ajlls\t éjij']; BAR = [ygnàgyààynygbjrbjrb] 

If you do not need to export them, you can use declare instead of export .

Really simplified version (if you do not need extensibility, there is one variable for processing, etc.) It will look like this:

 $ ssh user@somehost.com 'read foo' <<< "$foo" 
0
Jul 10 '19 at 12:53 on
source share

Escaping a variable to access variables outside the ssh session: ssh pvt@192.168.1.133 "~ / tools / myScript.pl \ $ BUILD_NUMBER"

-one
Aug 29 '17 at 7:36 on
source share



All Articles