Get execute result of su

I wrote a shell script.

 
su user << EOF
some commands
result=$?
EOF
 
echo $result
Other program determined by $result
 

It outputs nothing.

How to pass the variable 'result' "inside su" to "outside su"?

try echoing the value like

RETURN_VAL=`su user << EOF
some commands
result=$?
echo $result
EOF`

echo $RETURN_VAL

Thank you for your reply
I tried your method but the output is my whole code.

su user << EOF
some commands
result=$?
echo $result
EOF

I tried another one

 
RETURN_VAL=$(su user << EOF
some commands
result=$?
echo $result
EOF)
 
echo $RETURN_VAL

but it is still nothing not 0 or some error number
what I did wrongly?

Any guys here could help for my problem?
thanks a lot.

#! /bin/bash

echo $?
su user
echo $?

the running result is,

-bash-3.00$ ./su.sh
0
su: user user does not exist
1

$? can be helpful?

put the commands in a script then run. you really want to use the su - option to get a clean environment.

su - user -c "/foo/bar/command.script.sh"

if [[ $? -ne 0 ]]
then
   echo "bad return code"
fi