jeffreyveon
jeffreyveon

Reputation: 13830

xargs - use EOF as delimeter

How do I use the EOF as a delimeter in xargs such that the entire file is read?

E.g.

cat foo.py | xargs -d EOF -I % python -c %

I know there are other ways to get the above example to work, but I am interested in learning how to do the same with xargs.

Upvotes: 0

Views: 1070

Answers (2)

Kaz
Kaz

Reputation: 58578

For xargs to read an entire file and turn it into a single argument would be a useless behavior.

What you're trying to do with:

# replace % with the contents of foo.py and pass as an argument
cat foo.py | xargs -d EOF -I % python -c %

can be done like this:

# pass contents of foo.py as a single argument
python -c "$(cat foo.py)"

but what is the point, since you can do:

python foo.py

Upvotes: 1

ruakh
ruakh

Reputation: 183301

Since command-line arguments can never contain null bytes, your question essentially presupposes that your input does not contain null bytes. As a result, the simplest approach is to use null bytes as the delimiter, thereby guaranteeing that the entire input will be treated as a single item.

To do that, use the --null or -0 option:

cat foo.py | xargs --null -I % python -c %

Or, more tersely:

xargs -0  python -c  < foo.py

That said, I can't picture what this is useful for. If you know that you will only ever have one input item, then why use xargs at all? Why not just write

python -c "$(< foo.py)"

?

Upvotes: 3

Related Questions