Ovesh
Ovesh

Reputation: 5369

How do I pass a list of values to xargs as a single argument list?

I know I can use xargs to get a list of files and run a command once for each one of those files.

What I'd like to do is pass a list of files as single argument list to a command. For example:

ls | tail -10

outputs

a
b
c
d

but I'd like to pipe it to a command the will receive the list like this:

command a b c d

I'm guessing xargs is the best tool for the job, but I'm open for anything. The simpler to remember the better.

Upvotes: 4

Views: 3173

Answers (3)

perreal
perreal

Reputation: 97938

Just use quotes and variable expansion:

command "$(ls | tail)"

to test (command):

#!/bin/bash
echo $1

Output:

 a b c d

Upvotes: 3

Kent
Kent

Reputation: 195039

You could try:

command $(ls|tail -10|paste -s)

if you want to have space as separator:

command $(ls|tail -10|paste -s -d" ")

also xargs has a -n option, you could give a shot. for example:

kent$  seq 5 
1
2
3
4
5

kent$  seq 5|xargs -n5
1 2 3 4 5

Upvotes: 1

Armali
Armali

Reputation: 19375

Just

ls|xargs command

will do. Of course, if the list doesn't exceed the command line length limit,

command `ls`

also suffices.

Upvotes: 1

Related Questions