Shell – How to execute a command repeatedly with different arguments

argumentscommand lineshellxargs

I'm on Ubuntu. I copied some arguments (separated by newline) and I can use xsel to print them out like this

$ xsel
arg1
arg2
arg3
arg4
...

Now, I want to use each of these arguments for another command and execute that command as many times as there are arguments.

So I tried

$ xsel | mycommand "constantArgument" $1

However, this executed mycommand only for the first argument. How can I execute it for every argument?

Best Answer

You can simply use xargs

xsel | xargs -n1 echo mycommand 

-n1 means one arg for mycommand, but it's just dry run, it will show what going to be run, to run it remove echo

For constant Argument

xsel | xargs -I {} -n1 echo mycommand "constantArgument" {}