Can xargs' default delimiter be changed?

Sridhar Sarnobat picture Sridhar Sarnobat · Oct 6, 2013 · Viewed 27k times · Source

I want the following behavior without having to explicitly specify it with options:

xargs -d '\n'

Unlike with most commands, you can't just use an alias because pipes don't recognize aliases (as a side-note, why is it designed this way?). I also tried creating my own ~/bin/xargs script but I think it's not as simple as reading "$@" as a string inside the script.

Any suggestions how to make the delimiter a newline by default? I don't want to get a bunch of errors when I have a space in the path (and using find ... -print0 | xargs -0 has other unwanted effects).

UPDATE

My shell script attempt is this:

/usr/local/bin/xargs -d '\n' "$@"

Answer

Kelvin Edmison picture Kelvin Edmison · Nov 4, 2013

I tested xargs -d '\n' -n1 echo on the command line and it worked as advertised; breaking the input up into single lines and echoing it.

You could be being bitten by assumptions regarding shell escaping during variable assignment. For instance, if your shell script is similar to this:

CMD="xargs -d '\n' -n1 echo"
cat inputfile | $CMD

then an error occurs, because the double-quotes around the entire CMD string preserve both the single-quotes and the backslash in the delimiter.

If you are enclosing the xargs command within quotes, you could change it to the following (no single-quotes)

CMD="xargs -d \n -n1 echo"
cat inputfile | $CMD

and it will likely function as you need.

Tested on CentOS 6.4, xargs version 4.4.2