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/dyn.master.setenv.sh --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 master.example.com) and set a shell variable locally at slave.example.com?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Bash/Ksh
Unix
Est. reading time N/A
You need to use eval or source command as follows to fix your problem:

source

OR

eval $(ssh user@master cat /path/to/varfile)

Examples

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

JAIL=/lighttpd
PHP=/opt/bin/php5-cgi
MINIMAIL=/opt/bin/chroot-sendmail

Type the following command on server/workstation called slave.example.com (please note that [user@slave ~]$ is your prompt):

[user@slave ~]$ source 
[user@slave ~]$ echo "$JAIL $PHP $MINIMAIL"

OR

[user@slave ~]$ eval $(ssh user@master.example.com 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 master.example.com and slave.example.com Server.

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


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 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> for code samples. Still have questions? Post it on our forum