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

ww‮.w‬lautturi.com
SSH: Use Remote Bash / KSH source Command Set Variable Locally From a Remote Server

To use the source command to set a variable locally from a remote server using ssh, you can use the ssh command with the -t option to force pseudo-tty allocation, and the source command as the argument.

For example, to set a variable FOO locally from the remote server example.com, you can use the following command:

ssh -t example.com "source /path/to/script; echo \$FOO"

This will connect to the example.com server, run the source /path/to/script command, which should set the FOO variable, and then print the value of FOO to the terminal.

You can then use the eval command to assign the value of FOO to a local variable:

eval "$(ssh -t example.com "source /path/to/script; echo \$FOO")"

This will assign the value of FOO from the remote server to a local variable with the same name.

Keep in mind that this method only works if the source command is supported on the remote server and the /path/to/script file is accessible. You may also need to adjust the command for the specific shell being used on the remote server (e.g. bash, ksh, etc.).

For more information about using ssh to execute commands on a remote server, you can consult the ssh documentation or seek assistance from a qualified Linux or Unix administrator.

Created Time:2017-10-30 14:27:17  Author:lautturi