m4(1)

m41

Macro language processor

Usage

m4 [-EgPs] [-Dname [=value]] [-d flags] [-I dirname] [-o filename] [-t macro] [-Uname] [file ...]

Overview

The m4 utility is a macro processor that can be used as a front end to any language (e.g., C, ratfor, fortran, lex, and yacc). If no input files are given, m4 reads from the standard input, otherwise files specified on the command line are processed in the given order. Input files can be regular files, files in the m4 include paths, or a single dash (‘-’), denoting standard input. m4 writes the processed text to the standard output, unless told otherwise.

Macro calls have the form name(argument1[, argument2, ..., argumentN]).

There cannot be any space following the macro name and the open parenthesis (‘(’). If the macro name is not followed by an open parenthesis, it is processed with no arguments.

Macro names consist of a leading alphabetic or underscore possibly followed by alphanumeric or underscore characters, e.g., valid macro names match the pattern “[a-zA-Z_][a-zA-Z0-9_]*”.

In arguments to macros, leading unquoted space, tab, and newline (‘\n’) characters are ignored. To quote strings, use left and right single quotes (e.g., ` this is a string with a leading space'). You can change the quote characters with the changequote built-in macro.

Most built-ins don't make any sense without arguments, and hence are not recognized as special when not followed by an open parenthesis.

The options are as follows:

-Dname[=value]

Define the symbol name to have some value (or NULL).

-d flags

Set trace flags. flags may hold the following:

a

print macro arguments.

c

print macro expansion over several lines.

e

print result of macro expansion.

f

print filename location.

l

print line number.

q

quote arguments and expansion with the current quotes.

t

start with all macros traced.

x

number macro expansions.

V

turn on all options.

By default, trace is set to "eq".

-E

Set warnings to be fatal. When a single -E flag is specified, if warnings are issued, execution continues but m4 will exit with a non-zero exit status. When multiple -E flags are specified, execution will halt upon issuing the first warning and m4 will exit with a non-zero exit status. This behaviour matches GNU-m4 1.4.9 and later.

-g

Activate GNU-m4 compatibility mode. In this mode, translit handles simple character ranges (e.g., a-z), regular expressions mimic emacs behavior, multiple m4wrap calls are handled as a stack, the number of diversions is unlimited, empty names for macro definitions are allowed, and eval understands ‘0rbase:value’ numbers.

-I dirname

Add directory dirname to the include path.

-o filename

Send trace output to filename.

-P

Prefix all built-in macros with ‘m4_’. For example, instead of writing define, use m4_define.

-s

Output line synchronization directives, suitable for cpp(1).

-t macro

Turn tracing on for macro.

-Uname

Undefine the symbol name.

Exit status

The m4 utility exits 0 on success, and >0 if an error occurs.

But note that the m4exit macro can modify the exit status, as can the -E flag.

Releases

61 releases since OpenBSD 2.1; the text changed 28 times.

  1. 2.1–2.3
  2. 2.4–2.5
  3. 2.6
  4. 2.7
  5. 2.8
  6. 2.9
  7. 3.0–3.1
  8. 3.2
  9. 3.3
  10. 3.4
  11. 3.5
  12. 3.6
  13. 3.7–3.8
  14. 3.9
  15. 4.0
  16. 4.1
  17. 4.2–4.4
  18. 4.5–4.6
  19. 4.7
  20. 4.8
  21. 4.9–5.3
  22. 5.4
  23. 5.5
  24. 5.6–5.8
  25. 5.9–6.1
  26. 6.2–6.8
  27. 6.9–7.0
  28. 7.1
  29. 7.2–8.0

-E since OpenBSD 6.2; -I since OpenBSD 2.6; -P since OpenBSD 4.7; -d since OpenBSD 3.0; -g since OpenBSD 2.7; -o since OpenBSD 3.3; -s since OpenBSD 3.2; -t since OpenBSD 3.0.

Standards

The m4 utility is compliant with the IEEE Std 1003.1-2008 (“POSIX.1”) specification.

The flags [-dEgIPot] and the macros builtin, esyscmd, expr, format, indir, paste, patsubst, regexp, spaste, unix, __line__, and __file__ are extensions to that specification.

maketemp is not supposed to be a synonym for mkstemp, but instead to be an insecure temporary file name creation function. It is marked by IEEE Std 1003.1-2008 (“POSIX.1”) as being obsolescent and should not be used if portability is a concern.

The output format of traceon and dumpdef are not specified in any standard, are likely to change and should not be relied upon. The current format of tracing is closely modelled on gnu-m4, to allow autoconf to work.

The built-ins pushdef and popdef handle macro definitions as a stack. However, define interacts with the stack in an undefined way. In this implementation, define replaces the top-most definition only. Other implementations may erase all definitions on the stack instead.

All built-ins do expand without arguments in many other m4.

Many other m4 have dire size limitations with respect to buffer sizes.

Authors

Ozan Yigit ⟨[email protected]⟩ and Richard A. O'Keefe ⟨[email protected]⟩.

GNU-m4 compatibility extensions by Marc Espie ⟨[email protected]⟩.

From man 8.0-current-20261101 in OpenBSD 8.0-current (2026-11-01). Parsed by carta from the page's mdoc source.