How to modify environment variables before executing a command

Malte Forkel malte.forkel at berlin.de
Wed Dec 2 05:53:16 EST 2009


Hi,

I'd like to access and modify shell environment variables before
executing a command on a remote host via ssh. Unfortenatly, I can't
figure out the proper way to do this...

It seems, standard shell configuration files (e.g. ~/.bash_profile) are
not sourced before ssh executes a command. Of course I could explicitly
source them, e.g.
    ssh user at host 'source ~/.bash_profile; command'
But I don't like that because it delegates a server side configuration
task to the clients.

Is there a way to achieve this in a more general fashion on a per user
basis? I tried both ~/.ssh/environment and ~/.ssh/rc, as documented in
the man page. But ~/.ssh/environment only works for static variables and
I didn't manage to export variables defined in ~/.ssh/rc.

For example, if I have these files in my home directory on server
    # ~/.ssh/environment
    VAR1=$(date)
and
    # ~/.ssh/rc
    VAR2=$(date)
    export VAR2
then executing env from the client via
    ssh malte at server env | grep VAR
will result in
    VAR1=$(date)
i.e., VAR1 is passed literally while VAR2 is not available.

And where are the variables defined that I see when exexuting
    ssh user at server env
Among them are
    SHELL=/bin/bash
    PWD=/home/malte

Thanks in advance,
Malte



More information about the openssh-unix-dev mailing list