SSH: Use Remote Bash / KSH source Command Set Variable Locally From a Remote Server

Posted on in Categories , , last updated December 4, 2012

I have setup a master / local server combination using OpenSSH. My local server depends upon master server for a remote sourcing. The remote sourcing should set a large number of variables locally in a bash shell script. The code is:

[email protected] $ source ssh [email protected] "/service/ --mode=slave"
echo “$var”

The var is not setting and I am getting various errors. How can I do a remote “source” of a bash script file (from and set a shell variable locally at

You need to use eval or source command as follows to fix your problem:

source <(ssh [email protected] cat /path/to/varfile)


eval $(ssh [email protected] cat /path/to/varfile)


Create a file called /tmp/foo on master server called as follows:


Type the following command on server/workstation called (please note that [[email protected] ~]$ is your prompt):

[[email protected] ~]$ source <(ssh [email protected]  cat /tmp/foo)
[[email protected] ~]$ echo "$JAIL $PHP $MINIMAIL"


[[email protected] ~]$ eval $(ssh [email protected] cat /tmp/foo)
[[email protected] ~]$ echo "$JAIL $PHP $MINIMAIL"

Sample session

Following sample session uses localhost computer as a master and slave. However, the concept remains same for and Server.

Fig.01: Bash remote source or eval  command to set variables locally
Fig.01: Bash remote source or eval command to set variables locally (click to enlarge)

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Leave a Comment