Probably you are calling each DB2 command in different subshells. You can fix that problem by executing everything in just one subshell, for example
VALUE=$(. /home/db2inst1/sqllib/db2profile ; db2 connect to MYDB ; db2 "select * from table(myFoo())")