m4(1)

m41

Macro language processor

Usage

m4 [-EGgPs] [-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 do not 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=name[=value]

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

-D alone on OpenBSD.
-d [[+|-]flags], --debug=[[+|-]flags]

Set or unset trace flags. The trace flags are as follows:

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.

If "+" or "-" is used, the specified flags are added to or removed from the set of active trace flags, respectively; otherwise, the specified flags replace the set of active trace flags.

Specifying this option without an argument is equivalent to specifying it with the argument "aeq".

By default, trace is set to "eq".

-d alone on OpenBSD.
-E, --fatal-warnings

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 behavior matches GNU m4 1.4.9 and later.

-E alone on OpenBSD.
-G, --traditional

Disable GNU compatibility mode (see -g below).

Not on OpenBSD.
-g, --gnu

Enable GNU 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, undivert can be used to include files, and eval understands ‘0rbase:value’ numbers.

-g alone on OpenBSD.
-I dirname, --include=dirname

Add directory dirname to the include path.

-I alone on OpenBSD.
-o filename, --error-output=filename

Send trace output to filename.

Not on Linux. -o alone on OpenBSD.
-P, --prefix-builtins

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

-P alone on OpenBSD.
-s, --synclines

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

-s alone on OpenBSD.
-t macro, --trace=macro

Turn tracing on for macro.

-t alone on OpenBSD.
-Uname, --undefine=name

Undefine the symbol name.

-U alone on OpenBSD.

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

87 releases since FreeBSD 2.0.5-RELEASE; the text changed 54 times.

  1. 2.0.5
  2. 2.1.5–2.1.7.1
  3. 2.2.2–2.2.6
  4. 2.2.7–2.2.8
  5. 2.2.9
  6. 3.0
  7. 3.1–3.2
  8. 3.3–3.4
  9. 3.5–3.5.1
  10. 4.0–4.2
  11. 4.3
  12. 4.4–4.5
  13. 4.6–4.6.2
  14. 4.7
  15. 4.8
  16. 4.9–4.11
  17. 5.0
  18. 5.1
  19. 5.2–5.2.1
  20. 5.3–5.5
  21. 6.0–6.3
  22. 6.4
  23. 7.0
  24. 7.1
  25. 7.2
  26. 7.3
  27. 7.4
  28. 8.0
  29. 8.1
  30. 8.2
  31. 8.3
  32. 8.4
  33. 9.0
  34. 9.1
  35. 9.2
  36. 9.3
  37. 10.0
  38. 10.1
  39. 10.2
  40. 10.3
  41. 10.4
  42. 11.0
  43. 11.1
  44. 11.2
  45. 11.3
  46. 11.4
  47. 12.0
  48. 12.1
  49. 12.2
  50. 12.3
  51. 12.4
  52. 13.0
  53. 13.1–13.2
  54. 13.3–14.5
  55. 15.0–16.0

--debug since FreeBSD 13.3-RELEASE; --define since FreeBSD 13.3-RELEASE; --error-output since FreeBSD 13.3-RELEASE; --fatal-warnings since FreeBSD 13.3-RELEASE; --gnu since FreeBSD 13.3-RELEASE; --include since FreeBSD 13.3-RELEASE; --prefix-builtins since FreeBSD 13.3-RELEASE; --synclines since FreeBSD 13.3-RELEASE; --trace since FreeBSD 13.3-RELEASE; --traditional since FreeBSD 13.3-RELEASE; --undefine since FreeBSD 13.3-RELEASE; -E since FreeBSD 12.1-RELEASE; -G since FreeBSD 13.3-RELEASE; -I since FreeBSD 4.7-RELEASE; -P since FreeBSD 9.3-RELEASE; -d since FreeBSD 4.7-RELEASE; -g since FreeBSD 4.7-RELEASE; -o since FreeBSD 9.3-RELEASE; -s since FreeBSD 4.6-RELEASE; -t since FreeBSD 4.7-RELEASE.

Standards

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

The flags [-dEGgIPot] 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 base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.