M4(1P)
๐ M4(1P) โ POSIX Programmer's Manual ๐
okay bestie, buckle up, because we're about to get into m4 and it's honestly so much more than it looks ๐
this is the POSIX Programmer's Manual page for it, so every single detail matters, no skimming allowed. โจ
๐ PROLOG ๐
ok so before literally anything else, the important disclaimer:
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ โ so pls consult the corresponding Linux manual page for the details on Linux behavior โ or, and I cannot stress this enough, the interface may not be implemented on Linux at all. ๐ญ
๐ธ NAME ๐ธ
m4 โ macro processor. that's it. that's the whole vibe. a macro processor. iconic. ๐
โจ SYNOPSIS โจ
m4 [-s] [-D name[=val]]... [-U name]... file...
๐ฆ DESCRIPTION ๐ฆ
So the m4 utility is a macro processor, right? What it does is read one or more text files, process them according to their included macro statements, and write the results to standard output. that's the whole main character arc. ๐ฅ
๐ OPTIONS ๐
The m4 utility shall conform to the Base Definitions volume of POSIX.1โ2017, Section 12.2, Utility Syntax Guidelines โ except (and this is the plot twist) that the order of the -D and -U options shall be significant, and options can be interspersed with operands. so like, order matters here, bestie, don't get it twisted. ๐
The following options shall be supported:
-sโ Enable line synchronization output for thec99preprocessor phase (that is,#linedirectives). yes queen, give us the#linedirectives. โจ-D name[=val]โ Define name to val, or to null if=valis omitted. no=val? then it's just null, and that's on periodt.-U nameโ Undefine name. gone. deleted. not me erasing a macro. ๐
๐ซถ OPERANDS ๐ซถ
The following operand shall be supported:
fileโ A pathname of a text file to be processed. If no file is given, or if it is-, the standard input shall be read. yes,-is the classic "read stdin" move, iykyk. ๐
๐ STDIN ๐
The standard input shall be a text file that is used if no file operand is given, or if it is -. so basically stdin is the understudy that goes on whenever nobody names a file. ๐
๐ธ INPUT FILES ๐ธ
The input file named by the file operand shall be a text file. text only, bestie. no funny business. โจ
๐ ENVIRONMENT VARIABLES ๐
The following environment variables shall affect the execution of m4:
LANGโ Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1โ2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) it's giving backup default energy. ๐ซถLC_ALLโ If set to a non-empty string value, override the values of all the other internationalization variables. main character energy fr fr, it overrides everybody. ๐LC_CTYPEโ Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files). ๐ฆLC_MESSAGESโ Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. ๐NLSPATHโ Determine the location of message catalogs for the processing ofLC_MESSAGES. ๐
๐ ASYNCHRONOUS EVENTS ๐
Default. yeah. that's literally the whole section. nothing to see here, moving on bestie. ๐
๐ STDOUT ๐
The standard output shall be the same as the input files, after being processed for macro expansion. so it's the input, but make it fashion. โจ
๐ STDERR ๐
The standard error shall be used to display strings with the errprint macro, macro tracing enabled by the traceon macro, the defined text for macros written by the dumpdef macro, or for diagnostic messages. basically stderr is where all the ~drama~ lives. ๐ญ
๐ OUTPUT FILES ๐
None. nothing. zero. the void. ๐ณ๏ธ
๐ฅ EXTENDED DESCRIPTION ๐ฅ
okay so here's the actual lore, bestie. The m4 utility shall compare each token from the input against the set of built-in and user-defined macros. If the token matches the name of a macro, then the token shall be replaced by the macro's defining text, if any, and rescanned for matching macro names. Once no portion of the token matches the name of a macro, it shall be written to standard output. Macros may have arguments, in which case the arguments shall be substituted into the defining text before it is rescanned. it keeps rescanning until nothing matches anymore โ obsessed with the persistence honestly. ๐
Macro calls have the form:
name(arg1, arg2, ..., argn)
Macro names shall consist of letters, digits, and underscores, where the first character is not a digit. Tokens not of this form shall not be treated as macros. so like, no starting with a digit, that's the rule. periodt. โจ
The application shall ensure that the <left-parenthesis> immediately follows the name of the macro. If a token matching the name of a macro is not followed by a <left-parenthesis>, it is handled as a use of that macro without arguments.
If a macro name is followed by a <left-parenthesis>, its arguments are the <comma>-separated tokens between the <left-parenthesis> and the matching <right-parenthesis>. Unquoted white-space characters preceding each argument shall be ignored. All other characters, including trailing white-space characters, are retained. <comma> characters enclosed between <left-parenthesis> and <right-parenthesis> characters do not delimit arguments. so leading whitespace gets ghosted ๐ป but trailing whitespace stays, and commas inside parens are just, like, commas, not separators. got it? ๐ซถ
Arguments are positionally defined and referenced. The string "$1" in the defining text shall be replaced by the first argument. Systems shall support at least nine arguments; only the first nine can be referenced, using the strings "$1" to "$9", inclusive. The string "$0" is replaced with the name of the macro. The string "$#" is replaced by the number of arguments as a string. The string "$*" is replaced by a list of all of the arguments, separated by <comma> characters. The string "$@" is replaced by a list of all of the arguments separated by <comma> characters, and each argument is quoted using the current left and right quoting strings. The string "${" produces unspecified behavior.
ngl, "${" producing unspecified behavior is giving "don't test me, I will not be predictable" energy. ๐
If fewer arguments are supplied than are in the macro definition, the omitted arguments are taken to be null. It is not an error if more arguments are supplied than are in the macro definition. extra args? fine. missing args? they're just null, no big deal, we're chill here. ๐ธ
No special meaning is given to any characters enclosed between matching left and right quoting strings, but the quoting strings are themselves discarded. By default, the left quoting string consists of a grave accent (backquote) and the right quoting string consists of an acute accent (single-quote); see also the changequote macro. so by default it's ` and ' doing the quoting, and the quote marks themselves get yeeted. โจ
Comments are written but not scanned for matching macro names; by default, the begin-comment string consists of the <number-sign> character and the end-comment string consists of a <newline>. See also the changecom and dnl macros. comments pass through untouched, they don't get macro-checked. they're just vibing. ๐ฆ
The m4 utility shall make available the following built-in macros. They can be redefined, but once this is done the original meaning is lost. Their values shall be null unless otherwise stated. In the descriptions below, the term defining text refers to the value of the macro: the second argument to the define macro, among other things. Except for the first argument to the eval macro, all numeric arguments to built-in macros shall be interpreted as decimal values. The string values produced as the defining text of the decr, divnum, incr, index, len, and sysval built-in macros shall be in the form of a decimal-constant as defined in the C language.
heads up bestie: redefine a built-in and the original meaning is gone gone, like it never existed. don't say I didn't warn you. ๐
changecomโ Thechangecommacro shall set the begin-comment and end-comment strings. With no arguments, the comment mechanism shall be disabled. With a single non-null argument, that argument shall become the begin-comment and the<newline>shall become the end-comment string. With two non-null arguments, the first argument shall become the begin-comment string and the second argument shall become the end-comment string. The behavior is unspecified if either argument is provided but null. Systems shall support comment strings of at least five characters. so: zero args = comments off, one arg = that arg + newline, two args = begin + end. and yes, five characters minimum, systems gotta support that. ๐changequoteโ Thechangequotemacro shall set the begin-quote and end-quote strings. With no arguments, the quote strings shall be set to the default values (that is,`'). The behavior is unspecified if there is a single argument or either argument is null. With two non-null arguments, the first argument shall become the begin-quote string and the second argument shall become the end-quote string. Systems shall support quote strings of at least five characters. one arg or a null arg? unspecified, so like, don't. ๐decrโ The defining text of thedecrmacro shall be its first argument decremented by 1. It shall be an error to specify an argument containing any non-numeric characters. The behavior is unspecified ifdecris not immediately followed by a<left-parenthesis>. minus one, that's the whole job. non-numeric arg = error, no cap. ๐ญdefineโ The second argument shall become the defining text of the macro whose name is the first argument. It is unspecified whether thedefinemacro deletes all prior definitions of the macro named by its first argument or preserves all but the current definition of the macro. The behavior is unspecified ifdefineis not immediately followed by a<left-parenthesis>. it's giving "new definition, who dis" and honestly the old ones may or may not still be lurking. ๐defnโ The defining text of thedefnmacro shall be the quoted definition (using the current quoting strings) of its arguments. The behavior is unspecified ifdefnis not immediately followed by a<left-parenthesis>. ๐ฆdivertโ Them4utility maintains nine temporary buffers, numbered 1 to 9, inclusive. When the last of the input has been processed, any output that has been placed in these buffers shall be written to standard output in buffer-numerical order. Thedivertmacro shall divert future output to the buffer specified by its argument. Specifying no argument or an argument of 0 shall resume the normal output process. Output diverted to a stream with a negative number shall be discarded. Behavior is implementation-defined if a stream number larger than 9 is specified. It shall be an error to specify an argument containing any non-numeric characters. nine buffers, numbered 1 to 9, dumped to stdout in order at the end. negative stream = output gets yeeted into the void. >9 = implementation-defined, so like, roll the dice at your own risk. ๐ฒdivnumโ The defining text of thedivnummacro shall be the number of the current output stream as a string. just tellin' you which stream you're on, bestie. โจdnlโ Thednlmacro shall causem4to discard all input characters up to and including the next<newline>. it's the "delete to newline" button and it's lowkey so useful. ๐dumpdefโ Thedumpdefmacro shall write the defined text to standard error for each of the macros specified as arguments, or, if no arguments are specified, for all macros. no args = dump everything. chaos mode. ๐errprintโ Theerrprintmacro shall write its arguments to standard error. The behavior is unspecified iferrprintis not immediately followed by a<left-parenthesis>. ๐ขevalโ Theevalmacro shall evaluate its first argument as an arithmetic expression, using signed integer arithmetic with at least 32-bit precision. At least the following C-language operators shall be supported, with precedence, associativity, and behavior as described in Section 1.1.2.1, Arithmetic Precision and Operations:
()
unary +
unary -
~
and bestie, we are so not done with eval yet โ there's more where that came from. ๐
โจ
!binary */%binary +binary -<<>><<=>>===!=binary &^|&&||
Bestie, systems shall support octal and hexadecimal numbers, exactly like the ISO C standard does. Slay. โจ
The second argument, if you give one, sets the radix for the result โ and if that argument is blank or you just don't pass it at all, the default is 10. But heads up: behavior is unspecified if the radix falls outside the range 2 to 36, inclusive. So don't be chaotic with it. ๐ซถ
The third argument, if you give one, sets the minimum number of digits in the result. And behavior is unspecified if that third argument is less than zero. Also, it's an error to give a second or third argument containing any non-numeric characters. And the behavior is unspecified if eval is not immediately followed by a <left-parenthesis>. Noted, fr fr.
ifdefโ if the first argument is defined, the defining text is the second argument. Otherwise the defining text is the third argument if you gave one, or the null string if you didn't. Behavior is unspecified ififdefis not immediately followed by a<left-parenthesis>. ๐ifelseโ takes three or more arguments, okay?- If the first two arguments compare as equal strings (after macro expansion of both of them), the defining text is the third argument.
- If the first two arguments don't compare as equal strings and there are three arguments, the defining text is null.
- If the first two arguments don't compare as equal strings and there are four or five arguments, the defining text is the fourth argument.
- If the first two arguments don't compare as equal strings and there are six or more arguments, the first three arguments get discarded and processing restarts with the remaining arguments.
Behavior is unspecified if ifelse is not immediately followed by a <left-parenthesis>.
includeโ the defining text is the contents of the file named by the first argument. It's an error if the file can't be read. ๐ญ Behavior is unspecified ifincludeis not immediately followed by a<left-parenthesis>.incrโ the defining text is its first argument incremented by 1. It's an error to give an argument containing any non-numeric characters. Behavior is unspecified ifincris not immediately followed by a<left-parenthesis>.indexโ the defining text is the first character position (as a string) in the first argument where a string matching the second argument begins, zero origin, or-1if the second argument doesn't occur. Behavior is unspecified ifindexis not immediately followed by a<left-parenthesis>.lenโ the defining text is the length (as a string) of the first argument. Behavior is unspecified iflenis not immediately followed by a<left-parenthesis>.m4exitโ exit from them4utility, bye bestie ๐. If the first argument is given, that's the exit code. If no argument is given, the exit code is zero. It's an error to give an argument containing any non-numeric characters. And if the first argument is zero, or no argument is given, and an error has previously occurred (like, say, a file operand that couldn't be opened), it is unspecified whether the exit status is zero or non-zero. Sneaky.m4wrapโ the first argument gets processed when EOF is reached. If you use them4wrapmacro multiple times, the arguments you gave get processed in the order in which them4wrapmacros were processed. Behavior is unspecified ifm4wrapis not immediately followed by a<left-parenthesis>.maketempโ the defining text is the first argument, with any trailing'X'characters replaced with the current process ID as a string. Behavior is unspecified ifmaketempis not immediately followed by a<left-parenthesis>.mkstempโ the defining text is as if it were the resulting pathname after a successful call to themkstemp()function defined in the System Interfaces volume of POSIX.1โ2017, called with the first argument to the macro invocation. If a file is created, that file gets closed. ๐ If a file could not be created, them4utility writes a diagnostic message to standard error and keeps processing input, but its final exit status is non-zero โ and the defining text of the macro is the empty string. Behavior is unspecified ifmkstempis not immediately followed by a<left-parenthesis>.popdefโ deletes the current definition of its arguments, replacing that definition with the previous one. If there's no previous definition, the macro is undefined. Behavior is unspecified ifpopdefis not immediately followed by a<left-parenthesis>.pushdefโ equivalent to thedefinemacro, except it preserves any current definition for future retrieval using thepopdefmacro. Behavior is unspecified ifpushdefis not immediately followed by a<left-parenthesis>. It's giving "save file before you overwrite," iconic.shiftโ the defining text is a comma-separated list of its arguments except the first one. Each argument gets quoted using the current quoting strings. Behavior is unspecified ifshiftis not immediately followed by a<left-parenthesis>.sincludeโ equivalent to theincludemacro, except it's not an error if the file is inaccessible. Chill vibes. Behavior is unspecified ifsincludeis not immediately followed by a<left-parenthesis>.substrโ the defining text is the substring of the first argument beginning at the zero-offset character position specified by the second argument. The third argument, if given, is the number of characters to select; if you don't give one, the characters from the starting point to the end of the first argument become the defining text. It is not an error to give a starting point beyond the end of the first argument, and in that case the defining text is null. It is an error to give an argument containing any non-numeric characters. Behavior is unspecified ifsubstris not immediately followed by a<left-parenthesis>.syscmdโ interprets its first argument as a shell command line. The defining text is the string result of that command. That string result shall not be rescanned for macros while setting the defining text. No output redirection is performed by them4utility. The exit status value from the command can be retrieved using thesysvalmacro. Behavior is unspecified ifsyscmdis not immediately followed by a<left-parenthesis>. ๐sysvalโ the defining text is the exit value of the utility last invoked by thesyscmdmacro, as a string.traceonโ enables tracing for the macros specified as arguments, or, if no arguments are specified, for all macros. The trace output is written to standard error in an unspecified format. ๐traceoffโ disables tracing for the macros specified as arguments, or, if no arguments are specified, for all macros.translitโ the defining text is the first argument with every character that occurs in the second argument replaced with the corresponding character from the third argument. If no replacement character is specified for some source character because the second argument is longer than the third argument, that character gets deleted from the first argument intranslit's defining text. ๐
Behavior is unspecified if the '-' character appears within the second or third argument anywhere besides the first or last character. Behavior is unspecified if the same character appears more than once in the second argument. And behavior is unspecified if translit is not immediately followed by a <left-parenthesis>.
undefineโ deletes all definitions (including those preserved using thepushdefmacro) of the macros named by its arguments. Behavior is unspecified ifundefineis not immediately followed by a<left-parenthesis>.undivertโ causes immediate output of any text in temporary buffers named as arguments, or all temporary buffers if no arguments are specified. Buffers can be undiverted into other temporary buffers. Undiverting discards the contents of the temporary buffer. Behavior is unspecified if an argument contains any non-numeric characters. ๐ฆ
๐ EXIT STATUS ๐
The following exit values shall be returned:
0โ successful completion. โจ>0โ an error occurred. ๐ญ
If the m4exit macro is used, the exit value can be specified by the input file.
๐ธ CONSEQUENCES OF ERRORS ๐ธ
Default.
The following sections are informative.
๐ APPLICATION USAGE ๐
okay bestie so the defn macro? iconic for renaming macros, especially the built-ins. that's literally its whole thing. โจ
now, since eval defers to the ISO C standard, some operations have undefined behavior, and we do NOT play with those ๐. in some implementations, division or remainder by zero cause a fatal signal โ even if that division happens on the short-circuited branch of && or ||. like, rude. any operation that overflows in signed arithmetic produces undefined behavior. likewise, using the shift operators with a shift amount that is not positive and smaller than the precision is undefined, and so is shifting a negative number to the right. no cap, just don't.
and historically? not all implementations obeyed C-language precedence rules, which is giving chaos:
~and!were lower than====and!=were not lower than<|was not lower than^
the liberal use of () can force the desired precedence even with these non-compliant implementations. so like, parenthesize everything, it's free ๐
.
furthermore, some traditional implementations treated ^ as an exponentiation operator, although most implementations now use ** as an extension for this purpose. glow up era fr.
when a macro has been multiply defined via thepushdefmacro, it is unspecified whether thedefinemacro will alter only the most recent definition (as though bypopdefandpushdef), or replace the entire stack of definitions with a single definition (as though byundefineandpushdef). an application desiring particular behavior for thedefinemacro in this case can redefine it accordingly. ๐
applications should use the mkstemp macro instead of the obsolescent maketemp macro for creating temporary files. maketemp is so last season, bestie, we're not doing that anymore.
๐ธ EXAMPLES ๐ธ
if the file m4src contains the lines:
The value of `VER' is "VER".
ifdef(`VER', ``VER'' is defined to be VER., VER is not defined.)
ifelse(VER, 1, ``VER'' is `VER'.)
ifelse(VER, 2, ``VER'' is `VER'., ``VER'' is not 2.)
end
then the command
m4 m4src
or the command:
m4 -U VER m4src
produces the output:
The value of VER is "VER".
VER is not defined.
VER is not 2.
end
the command:
m4 -D VER m4src
produces the output:
The value of VER is "".
VER is defined to be .
VER is not 2.
end
the command:
m4 -D VER=1 m4src
produces the output:
The value of VER is "1".
VER is defined to be 1.
VER is 1.
VER is not 2.
end
the command:
m4 -D VER=2 m4src
produces the output:
The value of VER is "2".
VER is defined to be 2.
VER is 2.
end
like, look at that transformation arc. character development. ๐ฆ
๐ง RATIONALE ๐ง
historic System V-based behavior treated "${" in a macro definition as two literal characters. however, this sequence is left unspecified so that implementations may offer extensions such as "${11}" meaning the eleventh positional parameter. macros can still be defined with appropriate uses of nested quoting to result in a literal "${" in the output after rescanning removes the nested quotes. iykyk ๐
in the translit built-in, historic System V-based behavior treated - as a literal; GNU behavior treats it as a range. this version of the standard allows either behavior. both vibes are valid here, we're not picking sides. ๐
๐ฎ FUTURE DIRECTIONS ๐ฎ
None.
that's it. that's the whole section. the future is empty. we love a minimal queen. ๐
๐ SEE ALSO ๐
c99
the Base Definitions volume of POSIX.1โ2017, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines
๐ COPYRIGHT ๐
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
okay so that's the whole page, bestie, we did it ๐ซถ and the little footer at the bottom is from IEEE/The Open Group, dated 2017, for M4(1P) โ go forth and macro responsibly, you're the main character of this shell ๐ โจ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms