APPLY(1) General Commands Manual APPLY(1)

apply
apply a command to a list of arguments

apply [-ac] [-#] command arguments ...

apply divides its arguments into fixed-size groups and runs command in turn on each group.

On each execution of command, each character sequence of the form “%d” in command, where d is a digit from 1 to 9, is replaced with the d´th argument from the current argument group. The argument group size is set to the largest such d found. Any given argument number can be used arbitrarily many times. (Including zero.)

If no explicit substitution sequences are found in command, the current argument group is substituted after command delimited by spaces, and the argument group size defaults to 1 and can be set with the -# option.

If the argument group size is set to 0, one argument from arguments is taken for each execution of command anyway, but is discarded and not substituted; thus, command is run verbatim once for every argument.

The options are as follows:

-#
Set the argument group size. Ignored if explicit substitutions are used.
c
Change the magic substitution character from the default “%” to c.

The following environment variable affects the execution of apply:
Pathname of the shell to use to execute command. If this variable is not defined, the Bourne shell is used.

/bin/sh
Default shell.

Prints the name of every file in the current directory.
Compares the `a' files to the `b' files.
Runs who(1) 5 times.
Hard-links all files in the current directory into the directory /home/joe, with their names prefixed with "joe.".
Diff all C sources in the current directory against the last checked-in version and store each result in its own output file.

The apply command appeared in 4.2BSD.

Rob Pike

The complete command to be executed on each iteration is assembled as a string without additional quoting and then passed to a copy of the shell for parsing and execution. Thus, commands or arguments that contain spaces or shell metacharacters may behave in unexpected ways.

To protect a shell metacharacter fully it must be quoted twice, once against the current shell and once against the subshell used for execution. Similarly, for a shell metacharacter to be interpreted by the subshell it must be quoted to protect it from the current shell. A simple rule of thumb is to enclose the entire command in single quotes ('') so that the current shell does not interpret any of it.

There is no easy way to produce the literal string “%1” in command.

apply unconditionally inserts "exec" at the beginning of each copy of command so compound commands may not behave as intended.

March 12, 2016 NetBSD 9.4