(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
- The indexed array is filled (for convenience) with the names of the variables whose values you want to get on the remote side.
- 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. - 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"
Alice M. Jul 10 '19 at 12:53 on 2019-07-10 12:53
source share