It seems to me the command
for q in `seq 1 22`
do
DSS_QUERY=dss/templates ./qgen $q >> dss/queries/$q.sql
sed 's/^select/explain select/' dss/queries/$q.sql > dss/queries/$q.explain.sql
cat dss/queries/$q.sql >> dss/queries/$q.explain.sql;
done
adds the full query (without explain) to the end of the $q.explain.sql is that intentional? That way, your script appears to run the explain, then the query and then the query again.