use environment variables of remote server in ssh command

linux, shell, ssh

Solution

You can include your environment variables like following:

ssh user@host_y 'source ~/.bash_profile; /path/to/your/script/abc.sh'

Since direct command run is not an interactive shell, your variable will not work there. `source` will run script in your current shell and make visible environment variables in that file to your script.

Problem

I have two servers X(host) and Y(remote). A script abc.sh is saved on Y server. I am running abc.sh from server X using ssh command. Script is running successfully but the commands which contain the environment variables(kept in /.bash_profile) of server Y are giving blank output. When I am running abc.sh from server Y itself, all commands running successfully. My question is how to include the environment variables of remote server(Y) so that full script will execute successfully. NOTE : I don't have write access to etc directory so I can't change anything in it. Thanks in advance!

Original source

Related problems