Return of EXIT status ( $? )

I have the question:

How return the exit code from then assign :

VAR=$(command ) for ex. VAR=$(ls ....)

VAREXIT=$?

echo $VAREXIT

VAREXIT is equal to 0 if the directory exist or not exist. WHI??

if i execute the command direct from line-command , the value of $? is different if directory exist or not exist (0 or 2 )

for ex: $> ls direc./*
$> echo $?

return 2 if command return a error or 0 if command are execute without error

This should help...
basename=${0##*/}
trap 'print "$basename: Unexpected rc $? at line $LINENO"; exit 1' ERR # exits script for any non-zero return-code
trap '
#
# command to clean up on any script exit, any rc value
#
' EXIT