Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I ssh into a server and set an environment variable for the remote shell session? Ideally I'd like to control the name and value of the variable from the origin host.

share|improve this question

Use "SendEnv" configuration variable of ssh. see man ssh_config for more information.

share|improve this answer
Interesting. Never knew about that configuration variable. Nice to learn something new! – fpmurphy1 Aug 18 '09 at 14:03

SSH into the machine and then run export SHELLVAR=value. Put that into the .bashrc for the account if you want to make it permanent.

share|improve this answer
or /etc/profile for a global export. – David Rickman Aug 18 '09 at 3:44

If you're running a single-shot command on the remote side, something like this may work for you:

ssh user@remote "MYVAR=blah the_command -with args"

You can string multiple commands together as well:

ssh user@remote "export MYVAR=blah; one_command -with args && command2; command3"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.