Suppose that some variable FOO
is defined (and export
-ed) in the current environment. Let me call this environment E0
.
Now, I log into some other Unix system using ssh
.
% ssh [email protected]
Let me call the environment of this newly established session E1
.
In general, FOO
will not be defined in E1
, and, in the exceptional cases where it is, in general its value will be set independently of the value of FOO
in E0
.
How can I arrange things so that, upon log in via ssh
, the variable FOO
is set in E1
to the value it has in E0
(and, ideally, export
-ed in E1
as well)?
In case it matters, I'm using zsh
for both E0
and E1
.
For the answer to this question, assume that I do not have any control over the configuration of the sshd
process running on for.example.com
. This rules out, in particular, any solution that requires modifying /etc/ssh/sshd_config
, or the like, on for.example.com
.
Also, assume that the value of FOO
is not constant; it will change from one login session to another. In particular, FOO
may not be set in FOO
at all, in which case it is ok if the effect on E1
is analogous to that of FOO=
or export FOO=
or unset FOO
.
UPDATE: I tried this
% ssh [email protected] FOO=$FOO env
...and indeed, the output shows that FOO
is set as specified. But I have not figured out how to apply this idea when initiating a login session. Specifically, if I run
% ssh [email protected] FOO=$FOO
...the command just returns without establishing a connection (I figure that ssh
interprets FOO=$FOO
as the "command" to run on the remote host). On the other hand, after I run this
% ssh [email protected] FOO=$FOO zsh
...I never see the shell prompt from the remote host; the command just hangs there. (I do, however, get a password prompt from ssh
right before this.) The same thing happens for other variants of the last command, such as
% ssh [email protected] FOO=$FOO /path/to/zsh
% ssh [email protected] FOO=$FOO env zsh
% ssh [email protected] env FOO=$FOO zsh
% ssh [email protected] env FOO=$FOO zsh -l
% ssh [email protected] 'env FOO=$FOO zsh'
UPDATE2: Actually, what looked like "hanging" turns out to be that the prompt is not visible. I can still run commands. Also, the sequence of zsh initialization files that gets run upon login is different from normal, and (perhaps not suprisingly) the environment is substantially (and non-trivially, IOW above and beyond FOO
) different from normal.
UPDATE3: With help from dave_alcarin and meuh I found that this gets pretty close to what I am after:
% ssh -t [email protected] env FOO=$FOO zsh -l -s
There are still some non-trivial differences between the new environment and the normal one that I need to sort out, but this is pretty close to what I want.
ssh -t ... FOO=$FOO zsh
to have a pty.