How do I run a program with commandline arguments using GDB within a Bash script?

When running a program on GDB, usually, the arguments for the program are given at the run command. Is there a way to run the program using GDB and as well as give arguments within a shell script?

I saw an answer in a related question, mentioning that we can attach GDB to the program after the script starts executing. But then I will have to ‘wait’ the program.

Is there another way to do this?

9 s
9

You can run gdb with --args parameter:

gdb --args executablename arg1 arg2 arg3

If you are doing this often (e.g. when running GDB from a script), you might want to consider the following arguments to automate things further. First, you can place your GDB commands (such as ‘run’) in a text file and provide the filename to the -x argument. Second, you can have GDB exit after running your commands by providing the --batch argument. A full example:

gdb -x commands.txt --batch --args executablename arg1 arg2 arg3

Leave a Comment