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

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:


user@slave $ source ssh user@master "/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

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
You need to use eval or source command as follows to fix your problem:

source <(ssh user@server cat /path/to/varfile)


eval $(ssh user@master 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 [user@slave ~]$ is your prompt):

[user@slave ~]$ source <(ssh  cat /tmp/foo)
[user@slave ~]$ echo "$JAIL $PHP $MINIMAIL"


[user@slave ~]$ eval $(ssh cat /tmp/foo)
[user@slave ~]$ 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)

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.