Using xargs

Learn how to use xargs to use data from standard input as an argument for other functions.

The xargs command

The xargs program takes data from standard input and lets us use it as an argument to another program. Let’s do something incredibly trivial to understand how it works: print out the numbers 1 to 5 with echo, using xargs. We use the seq command, which prints out a sequence of numbers, each on its own line.

$ seq 5 | xargs echo

What actually happened here? The numbers 1 through 5 were passed as arguments to the echo command. We can see this with the -t argument, which prints the command to be executed before executing it:

$ seq 5 | xargs -t echo

If we don’t specify a command, xargs uses echo by default. Let’s test it out:

$ seq 5 | xargs -t

Use the terminal below to practice these commands.

Get hands-on with 1200+ tech skills courses.