HOWTO · Bash

How to Run GDB in a Bash Script

Compile a C program with debugging symbols, run it under GDB from Bash, pass program arguments, and automate debugging commands.

On this page

To run a program under GDB from Bash, invoke gdb with --args, then the executable and its arguments. Add -ex run when the script should start the program automatically; without it, GDB opens its prompt and waits for commands. This guide prepares a small C program, builds the command safely, and shows interactive, breakpoint, and command-file workflows. The runtime supplied for this article does not include GDB, so the debugger commands below are documented but are not presented as executed output.

Prepare a Program for GDB

The example below writes a small C program to a temporary directory, compiles it with GCC’s -g option, then runs it with an argument. The debug symbols let GDB associate machine instructions with source lines and variables. The temporary files are removed when the script exits.

set -eu
workdir=$(mktemp -d)
trap 'rm -rf "$workdir"' EXIT
cat > "$workdir/example.c" <<'C'
#include <stdio.h>

int main(int argc, char **argv) {
    if (argc > 1) {
        printf("Argument: %s\n", argv[1]);
    }
    return 0;
}
C
gcc -g -o "$workdir/example" "$workdir/example.c"
cd "$workdir"
program=./example
"$program" hello
printf 'gdb --quiet -ex run --args %s hello\n' "$program"
Argument: hello
gdb --quiet -ex run --args ./example hello

The executable accepts one optional value; here, hello is printed. Use the same executable path and argument after --args when starting GDB. The GDB commands below require GDB and the compiled executable on the machine running them.

Run GDB from a Bash Script

A Bash script can store the executable and arguments in variables, then pass them after --args. The preceding example previews the assembled command without requiring GDB to be installed. To start a real debugging session from a script, use gdb --quiet -ex run --args "$program" "$argument". The --quiet option suppresses GDB’s introductory message, while -ex runs the following GDB command. -ex run starts the program; omitting it starts GDB at the (gdb) prompt. To interact, omit -ex run and type run at that prompt. Quote variable expansions so arguments containing spaces remain a single argument. These debugger invocations require GDB and the compiled example executable on the machine running the script.

Set Breakpoints and Inspect Execution

To pause when the program enters main, add -ex 'break main' before -ex run, for example: gdb --quiet -ex 'break main' -ex run --args ./example hello. GDB stops at the breakpoint before executing the body of main. At the prompt, inspect the arguments with print argc and print argv[1], then continue with continue. The bt command prints the current call stack; it is useful while execution is paused, such as at a breakpoint or after a crash. A normal run that has already exited has no active call stack to inspect.

Automate GDB with a Command File

For repeatable debugger steps, put GDB commands in a plain text file named gdb_commands.txt:

break main

run

print argc

Load that file with gdb --quiet -x gdb_commands.txt --args ./example hello. GDB processes the listed commands in order, stopping at main, running the program, then printing argc. Use -ex for a short command sequence embedded in a Bash script; use -x when keeping a longer or reusable sequence in its own file. The command file contains GDB commands, while --args supplies the executable and the program’s arguments.