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.
sshdoes not (and can't) make such variables available on the remote side.You can instead embed the definition just like you embedded your function:
You can also copy all known variables. In that case it helps to squash the errors about setting read-only values:
If you have a specific variable you frequently want to copy, you can choose to have
sshsend it automatically by addingSendEnv envito your ssh config. The variable must be exported for this to work.