Reputation: 151
I have a script doing something like this:
var1=""
ssh xxx@yyy<<'EOF'
[...]
var2=`result of bash command`
echo $var2 #print what I need
var1=$var2 #is there a way to pass var2 into global var1 variable ?
EOF
echo $var1 # the need is to display the value of var2 created in EOF block
Is there a way to do this?
Upvotes: 0
Views: 1165
Reputation: 59516
In general, an executed command has three paths of delivering information:
It is not possible to change a (environment) variable of the parent process. This is true for all child processes, and your ssh
process is no exemption.
I would not rely on ssh
to pass the exit code of the remote process, though (because even if it works in current implementations, this is brittle; ssh
could also want to state its own success or failure with its exit code, not the remote process's).
Using files also seems inappropriate because the remote process will probably have a different file system (but if the remote and the local machine share an NFS for instance, this could be an option).
So I suggest using the output of the remote process for delivering information. You could achieve this like this:
var1=$(ssh xxx@yyy<<'EOF'
[...]
var2=$(result of bash command)
echo "$var2" 1>&2 # to stderr, so it's not part of the captured output
# and instead shown on the terminal
echo "$var2" # to stdout, so it's part of the captured output
EOF
)
echo "$var1"
Upvotes: 3