APPLY(1B) BSD System Compatibility APPLY(1B)

apply, pick - repeatedly apply a command to a group of arguments; select arguments

apply [ -v | -d ] [ -a m ] [ -# ] command args ...

pick [ -f ] [ args ... ]

Apply applies a command on specified args, each one at a turn. Normally, arguments are permutated per one, but one can change this per utilizing the -# option, which specifies the number of arguments that will be passed to command at once. If the number is zero, command will be executed with no arguments, but for the same number of times as there are args. Character sequences of the form "%#" in command, where # is also a number, this time limited betwixt 1 and 9, are replaced per its correspondent argument. If such sequences occur, the number indicated per the -# option shall be ignored and the maximum number of arguments passed to command will be the largest value of "%#". The magic character "%" can be changed per the -a option.

Pick writes each argument to the standard error output and reads a reply. If the reply is "y", pick will echo it to the standard output; if the reply is "q", pick exists without reading any more arguments; there is no error output for other replies, it just skips the current argument. If there are no arguments, lines of the standard input are taken instead, which can be particularly useful for using in pipes. The -f option can be useful for dealing with not-so standard filenames which contain spaces and/or non-alphanumeric symbols that are also reserved by the shell such as brackets, square brackets, braces and chevrons.

A time-consuming alternative to ls(1) with the -1 option:

% apply echo *

Same as before, this time globbing-safe:

% apply "echo '%1'" *

Compare pairs of files:

% apply -2 cmp a1 a2 b1 b2

Pick some songs from a folder and enqueue on the playlist in Qt-based Multimedia Player:

% find "/usr/home/luiz/Music/Cauby Peixoto" -type f -print | pick -f | xargs qmmp -e

Move commits from one branch to another, from the oldest to the newest, on the git(1) RCS utilizing the git-cherry-pick(1) utility.

% git log --oneline | pick | cut -d' ' -f1 > /tmp/to-cherrypick.txt
% git checkout mala # Another branch
% apply 'git cherry-pick' `tail -r /tmp/to-cherrypick.txt`

Pick '.c' and '.h' files and count the lines from each:

% wc -l `pick *.[ch]`

Same as before, with different processes for each file:

% apply 'wc -l' `pick *.[ch]`

Far from useful, but a really fun way of writing the lyrics for "99 bottles of beer", taking advantage of the fact that apply calls sh(1) per default:

% apply "s=''; [ %1 -ne 1 ] && s='s'; \
printf '%d bottle%c of beer on the wall, %d bottle%c of beer.\n' \
%1 \"$s\" %1 \"$s\"; \
printf 'Take one down and pass around,'; \
if [ -n $s ]; then \
printf ' %d bottle%c of beer on the wall.\n' `expr %1 - 1` \"$s\"; \
else \
printf ' no more bottles of beer on the wall!\n'; \
fi" `seq 99 -1 1` 

Path name to an alternative shell program to be used.

sh(1), xargs(1)

Because of the algorithm for applying every argument from args into the command string and then executing it via a system(3)-oid, it is slightly slower than something that could be crudely made as a sh(1) native function, but one shall not be bothered since the difference is pretty much negligible.

Pick is implemented, albeit it has a -f option as an extension to deal with not-so portable file names, intensively following its historical description at the AT&T Research Unix v10 manual page, which is the reason to it being extremely simple. Since nothing about it besides the description at the manual could be found, this implementation is just a reconstruction of the original concept and maybe differ in some small aspects.

Use special shell metacharacters at the command string with care, enclosing it in single quotes when needed; if not, one can get some bizarre results.

This could be seen more like a technical limitation of the way characters and strings work in the C programming language than a bug per se, but only plain ASCII magic characters are supported.

An apply command first appeared in 4.2BSD.
Then, via 4.2BSD, it got into Bell Labs' CSRC for the 8th edition of Research Unix and stayed there until the 10th edition, when it became accompanied by a pick command, which is very shady since an 10th edition of the Research Unix never existed uniformly and, just as other utilities, it's mentioned in the manual but neither its source nor binaries are present.
Per the 4.2BSD manual page for apply, the authorship is attributed to Rob Pike; just some guy who made part of small and fairly niche projects such as the Blit terminal, UTF-8, Plan 9, acme and sam editors, Limbo, Inferno and the Go Programming Language.

07/20/24 Heirloom Toolchest