I'm trying to learn how to manipulate pipe flow using xargs. Often times I need the output of my commands to be fed into commands that can't be piped into by stdout.

awk seems to have some equivalent to represent the entirety of standard out with the symbol $0 when you print with it, and $1,$2 etc are individual fields, but I can't seem to find the equivalent in xargs anywhere.

Example: Something like this: standard out is a list of arguments that are out of order:


secondfield firstfield thirdfield
secondfield2 firstfield2 thirdfield2
secondfield3 firstfield3 thirdfield3

I want to run pseudocode that looks like this:

cat file.txt | xargs myprogram $2 $1 $3

myprogram takes input fields in order where it should be the firstfield, secondfield, then thirdfield, but clearly my stdout is out of order. $1 $2 $3 only exist in awk. What is the equivalent?

(This is just a simplified example, but the point is that I need to know how to order my standard out stream into xargs in case I need to manipulate which varialbes go where in other complicated commands (mv for instance)

  • Pipe to awk, re-arrange, and then pipe to xargs?
    – muru
    Apr 22, 2016 at 20:10
  • 1
    Not sure how you'd do that with xargs... however you could always set -- $(some command) and then myprogram $2 $1 $3 just keep in mind word splitting applies... Apr 22, 2016 at 20:17

2 Answers 2


If you do:

cat file.txt | xargs myprogram

this will as if myprogram is called as:

myprogram secondfield firstfield thirdfield secondfield2 firstfield2 thirdfield2 secondfield3 firstfield3 thirdfield3

unless your arguments are really long and they cannot be passed on the commandline. That is not what you want to start out with. Instead do

cat file.txt | xargs --max-lines=1 myprogram

with that myprogram will be called with 3 parameters but with the fields in the wrong order. If you cannot change myprogram I would call a small python program:

#!/usr/bin/env python

import sys
import subprocess

sys.exit(subprocess.call('myprogram', sys.argv[2], sys.argv[1], sys.argv[3]))

in the following way:

cat file.txt | xargs --max-lines=1 mypythonprog 
$ xargs -d '\n' -L1 sh -c 'set $1; echo $2 $1 $3' xx  < file.txt 
firstfield secondfield thirdfield
firstfield2 secondfield2 thirdfield2
firstfield3 secondfield3 thirdfield3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .