Some tools accept many arguments at once. xargs -n groups input words into batches before running the command.

Program

Play the script to choose the batch size and see how the arguments are grouped.

xargs_batch_plan.sh
#!/usr/bin/env bash

batch_size=
items=("api" "worker" "web")
command="xargs -n $batch_size restart"
if [ "$batch_size" -eq 2 ]; then
    groups="api worker|web"
else
    groups="api worker web"
fi
echo "$command -> $groups"
#!/usr/bin/env bash

batch_size=
items=("api" "worker" "web")
command="xargs -n $batch_size restart"
if [ "$batch_size" -eq 2 ]; then
    groups="api worker|web"
else
    groups="api worker web"
fi
echo "$command -> $groups"
xargs `xargs` converts input words into command arguments.
batch size `-n` controls how many arguments go into each command run.
grouping Changing the batch size changes the command grouping without changing the input list.