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
gdb -ex=r --args myprogram arg1 arg2
-ex=r
is short for -ex=run
and tells gdb to run your program immediately, rather than wait for you to type "run" at the prompt. Then --args
says that everything that follows is the command and arguments, just as you'd normally type them at the commandline prompt.