22.3. Command files

A command file for gdb is a file of lines that are gdb commands. Comments (lines starting with #) may also be included. An empty line in a command file does nothing; it does not mean to repeat the last command, as it would from the terminal.

When you start gdb, it automatically executes commands from its init files, normally called .gdbinit[1]. During startup, gdb does the following:

  1. Reads the init file (if any) in your home directory[2].

  2. Processes command line options and operands.

  3. Reads the init file (if any) in the current working directory.

  4. Reads command files specified by the -x option.

The init file in your home directory can set options (such as set complaints) that affect subsequent processing of command line options and operands. Init files are not executed if you use the -nx option (refer to Section 4.1.2 Choosing modes).

On some configurations of gdb, the init file is known by a different name (these are typically environments where a specialized form of gdb may need to coexist with other forms, hence a different name for the specialized version's init file). These are the environments with special init file names:

You can also request the execution of a command file with the source command:

source filename

Execute the command file filename.

The lines in a command file are executed sequentially. They are not printed as they are executed. An error in any command terminates execution of the command file and control is returned to the console.

Commands that would ask for confirmation if used interactively proceed without asking when used in a command file. Many gdb commands that normally print messages to say what they are doing omit the messages when called from command files.

gdb also accepts command input from standard input. In this mode, normal output goes to standard output and error output goes to standard error. Errors in a command file supplied on standard input do not terminate execution of the command file -- execution continues with the next command.

gdb < cmds > log 2>&1

(The syntax above will vary depending on the shell used.) This example will execute commands from the file cmds. All output and errors would be directed to log.

Notes

[1]

The DJGPP port of gdb uses the name gdb.ini instead, due to the limitations of file names imposed by DOS filesystems.

[2]

On DOS/Windows systems, the home directory is the one pointed to by the HOME environment variable.