Ssh only passes only the TERM
environment variable through (+ LANG
and LC_*
in many default configs ^1). Changing the server's configuration to accept any environment variables can have serious consequences (allowing a user to bypass their login shell and any ForceCommand
), and you should not do it.
Instead of trying to pass them through ssh, you could set them on the right side, as part of the "remote command" argument(s), and start an interactive login shell explicitly:
ssh -t user@host AAA=tatata bash -l
The -t
option is to let ssh
allocate a pseudo-terminal, something it won't do by default when a remote command is specified. The -l
option of bash is to let it run a login shell, i.e. to let it source ~/.bash_login
, and simulate as closely as possible the way it's run when no remote command arguments are specified to ssh.
Since the values of the environment variables will be expanded twice, both on the local and on the remote side, you should double quote them, which may turn nasty fast:
ssh -t user@host PRICE=\\\$19 bash -l
But if your configuration allows the LC_*
envvars through, you could just name your variables that way ;-)
LC_AAA=tatata ssh user@host
or, if your ssh client config does not include LC_*
in SendEnv
:
LC_AAA=tatata ssh -o SendEnv=LC_AAA user@host
^1 In order to be able to pass extra envvars through ssh, both AcceptEnv
on the server and SendEnv
on the client are needed. Look for those directives in ssh*_config
to see what their defaults are on your system(s).
${var@Q}
is only "safe" to use if the login shell of user@host is bash, of the same version as on the local host and the OS and locale are the same on the two hosts. I'd avoid using any quoting operators that doesn't use only single quotes (use zsh's${(qq)VAR}
for instance). See Escape a variable for use as content of another script for details. – Stéphane Chazelas Oct 02 '20 at 17:28$'...'
, etc. – Oct 02 '20 at 17:32${(qq)VAR}
should be safe but only if the login shell of user@host is Bourne-like. More on that at How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user? – Stéphane Chazelas Oct 02 '20 at 17:37