How do I pipe a newline separated list as arguments to another command?
Use xargs
:
mycommand | xargs -L1 id
Example:
$ (echo root; echo nobody) | xargs -L1 id
uid=0(root) gid=0(root) groups=0(root)
uid=65534(nobody) gid=65534(nogroup) groups=65534(nogroup)
You can also loop over the input in bash:
mycommand | while read line
do
id "$line"
done
xargs
converts input to arguments of a command. The -L1
option tells xargs
to use each line as a sole argument to an invocation of the command.
With bash, you can capture the lines of output into an array:
mapfile -t lines < <(mycommand)
And then iterate over them
for line in "${lines[@]}"; do
id "$line"
done
This is not as concise as xargs, but if you need the lines for more than one thing, it's pretty useful.