Next: , Previous: Platform macros, Up: Shell commands


12.2 Executing simple commands

Any shell command can be executed, using syscmd:

— Builtin: syscmd (shell-command)

Executes shell-command as a shell command.

The expansion of syscmd is void, not the output from shell-command! Output or error messages from shell-command are not read by m4. See Esyscmd, if you need to process the command output.

Prior to executing the command, m4 flushes its buffers. The default standard input, output and error of shell-command are the same as those of m4.

The macro syscmd is recognized only with parameters.

     define(`foo', `FOO')
     =>
     syscmd(`echo foo')
     =>foo
     =>

Note how the expansion of syscmd keeps the trailing newline of the command, as well as using the newline that appeared after the macro.

As an example of shell-command using the same standard input as m4, the command line echo "m4wrap(\`syscmd(\`cat')')" | m4 will tell m4 to read all of its input before executing the wrapped text, then hand a valid (albeit emptied) pipe as standard input for the cat subcommand. Therefore, you should be careful when using standard input (either by specifying no files, or by passing `-' as a file name on the command line, see Invoking m4), and also invoking subcommands via syscmd or esyscmd that consume data from standard input. When standard input is a seekable file, the subprocess will pick up with the next character not yet processed by m4; when it is a pipe or other non-seekable file, there is no guarantee how much data will already be buffered by m4 and thus unavailable to the child.