You're using this in order to drive a script through gdb since you need to generate the output automatically rather than immediately interacting with the debugger.
The shell script is silly and needless overhead.
Instead of system("silly shell script"), use this;
FILE *gdbin = popen(gdbcmd, "w");
fprintf(gdbin, "set height 0\nbt full\n....");
pclose(gdbin);