Reputation: 15
Trying to pass local variable to remote shell using bash script. Here is what I am trying. This is test.sh
#!/bin/bash
envi=$1
function samplefunction {
echo "Environment selected is $envi"
}
if [ "$envi" = "test" ]; then
ssh user@remotehost <<EOF
$(typeset -f samplefunction)
samplefunction
EOF
else
echo "Please pass correct parameter which is - test"
fi
When I try to execute "./test.sh test" the result I am getting is "Environment selected is". shell is not able to pass the variable to the remote system.
Upvotes: 0
Views: 783
Reputation: 123680
ssh
does not (and can't) make such variables available on the remote side.
You can instead embed the definition just like you embedded your function:
ssh user@remotehost <<EOF
$(declare -p envi)
$(typeset -f samplefunction)
samplefunction
EOF
You can also copy all known variables. In that case it helps to squash the errors about setting read-only values:
ssh user@remotehost <<EOF
{
$(declare -p)
} 2> /dev/null
$(typeset -f samplefunction)
samplefunction
EOF
If you have a specific variable you frequently want to copy, you can choose to have ssh
send it automatically by adding SendEnv envi
to your ssh config. The variable must be exported for this to work.
Upvotes: 0