Ubuntu – How to pipe a newline separated list as arguments to another command

bashcommand linepipe

So I have a list of usernames such as:


I want to apply id on each of them and get something like:

uid=100(user1) gid=5(g1) groups=5(g1),6(g6),7(g10)

How can I achieve this? Please note that the list is the output of another command say mycommand.

Best Answer

Use xargs:

mycommand | xargs -L1 id


$ (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
    id "$line"

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.