APPLY(1) | General Commands Manual | APPLY(1)

# NAME

`apply` —
apply a command to a list of arguments

# SYNOPSIS

`apply` | [`-a` c] [`-`#] command arguments ...

# DESCRIPTION

`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.
`-a` c
  Change the magic substitution character from the default “`%`” to c.

# ENVIRONMENT

The following environment variable affects the execution of `apply`:

`SHELL`
  Pathname of the shell to use to execute command. If this variable is not defined, the Bourne shell is used.

# FILES

/bin/sh
  Default shell.

# EXAMPLES

`apply echo *`
  Prints the name of every file in the current directory.
`apply -2 diff a1 b1 a2 b2 a3 b3`
  Compares the `a' files to the `b' files.
`apply -0 who 1 2 3 4 5`
  Runs who(1) 5 times.
`apply ´ln %1 /home/joe/joe.%1´ *`
  Hard-links all files in the current directory into the directory /home/joe, with their names prefixed with "joe.".
`apply ´cvs diff %1 > %1.diff' *.c`
  Diff all C sources in the current directory against the last checked-in version and store each result in its own output file.

# HISTORY

The `apply` command appeared in 4.2BSD.

# AUTHORS

Rob Pike

# RESTRICTIONS

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.

# BUGS

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 10.0