2 added 120 characters in body
source | link

You can in principle send environment variables to the remote over SSH, but at least OpenSSH disallows it on the server, and doesn't try to send any from the client. The relevant configuration options are AcceptEnv in sshd_config (server) and SendEnv in ssh_config (client).

Assuming you can't or don't want to change the configuration there, since you're sending a whole script to the stdin on the remote anyway, you could also stick the variable assignments in front of that script.

We can use export -p to print out all the exported variables in the current shell, so something like this would setin a format that the remote shell can use to assign them atthere. Then the remote before runningcan process the script itself:as usual, with the variables set.

So with script.sh containing

$ cat script.sh
echo "$FOO" "$HOSTNAME"

This would export FOO in the local shell, and then pass the definitions of all exported variables to remote, along with the script itself. The resulting output contains the value of FOO that was just set.

$ export FOO=abc123
$ (export -p; cat script.sh) | ssh user@remotehost  'bash -s' 
FOO: abc123 HOSTNAME: remotehost

or with process substitution instead of a pipe:

$ ssh user@remotehost  'bash -s'  < <(export -p; cat script.sh)
FOO: abc123 HOSTNAME: remotehost

(There's a slight problem with Bash here. In normal mode, its export -p prints out declare commands instead of the standard export, and those may not be supported by other shells. So if your local shell is Bash, and you'd start a standard sh on the remote, you'd get errors.)

You can in principle send environment variables to the remote over SSH, but at least OpenSSH disallows it on the server, and doesn't try to send any from the client. The relevant configuration options are AcceptEnv in sshd_config (server) and SendEnv in ssh_config (client).

Assuming you can't or don't want to change the configuration there, since you're sending a whole script to the stdin on the remote anyway, you could also stick the variable assignments in front of that script.

We can use export -p to print out all the exported variables in the current shell, so something like this would set them at the remote before running the script itself:

$ cat script.sh
echo "$FOO" "$HOSTNAME"
$ export FOO=abc123
$ (export -p; cat script.sh) | ssh user@remotehost  'bash -s' 
FOO: abc123 HOSTNAME: remotehost

or with process substitution instead of a pipe:

$ ssh user@remotehost  'bash -s'  < <(export -p; cat script.sh)
FOO: abc123 HOSTNAME: remotehost

(There's a slight problem with Bash here. In normal mode, its export -p prints out declare commands instead of the standard export, and those may not be supported by other shells. So if your local shell is Bash, and you'd start a standard sh on the remote, you'd get errors.)

You can in principle send environment variables to the remote over SSH, but at least OpenSSH disallows it on the server, and doesn't try to send any from the client. The relevant configuration options are AcceptEnv in sshd_config (server) and SendEnv in ssh_config (client).

Assuming you can't or don't want to change the configuration there, since you're sending a whole script to the stdin on the remote anyway, you could also stick the variable assignments in front of that script.

We can use export -p to print out all the exported variables in the current shell, in a format that the remote shell can use to assign them there. Then the remote can process the script as usual, with the variables set.

So with script.sh containing

echo "$FOO" "$HOSTNAME"

This would export FOO in the local shell, and then pass the definitions of all exported variables to remote, along with the script itself. The resulting output contains the value of FOO that was just set.

$ export FOO=abc123
$ (export -p; cat script.sh) | ssh user@remotehost  'bash -s' 
FOO: abc123 HOSTNAME: remotehost

(There's a slight problem with Bash here. In normal mode, its export -p prints out declare commands instead of the standard export, and those may not be supported by other shells. So if your local shell is Bash, and you'd start a standard sh on the remote, you'd get errors.)

1
source | link

You can in principle send environment variables to the remote over SSH, but at least OpenSSH disallows it on the server, and doesn't try to send any from the client. The relevant configuration options are AcceptEnv in sshd_config (server) and SendEnv in ssh_config (client).

Assuming you can't or don't want to change the configuration there, since you're sending a whole script to the stdin on the remote anyway, you could also stick the variable assignments in front of that script.

We can use export -p to print out all the exported variables in the current shell, so something like this would set them at the remote before running the script itself:

$ cat script.sh
echo "$FOO" "$HOSTNAME"
$ export FOO=abc123
$ (export -p; cat script.sh) | ssh user@remotehost  'bash -s' 
FOO: abc123 HOSTNAME: remotehost

or with process substitution instead of a pipe:

$ ssh user@remotehost  'bash -s'  < <(export -p; cat script.sh)
FOO: abc123 HOSTNAME: remotehost

(There's a slight problem with Bash here. In normal mode, its export -p prints out declare commands instead of the standard export, and those may not be supported by other shells. So if your local shell is Bash, and you'd start a standard sh on the remote, you'd get errors.)