Existing DB2 connection does not work inside a script
As @mustaccio points out in the comment, a connection is local to its subshell.

In order to call the shell script in the context of the current shell, source could be used:

db2inst1@hostname:/tmp$ source test.sh

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.3
 SQL authorization ID   = DB2INST1
 Local database alias   = DBNAME


Of course, . also works:

db2inst1@hostname:/tmp$ . test.sh

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.3
 SQL authorization ID   = DB2INST1
 Local database alias   = DBNAME