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

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