Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can xargs' default delimiter be changed?

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' "$@"
like image 987
Sridhar Sarnobat Avatar asked Oct 06 '13 01:10

Sridhar Sarnobat


People also ask

What is the default command used by xargs?

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is echo) one or more times with any initial-arguments followed by items read from standard input.

What can I use instead of xargs?

If you can't use xargs because of whitespace issues, use -exec . Loops are just as inefficient as the -exec parameter since they execute once for each and every file, but have the whitespace issues that xargs have.

What is the difference between xargs and pipe?

What is the difference between pipe and xargs? pipes connect output of one command to input of another. xargs is used to build commands. so if a command needs argument passed instead of input on stdin you use xargs the default command is echo (vbe's example).

Can we use xargs and standard input?

xargs is a Unix command which can be used to build and execute commands from standard input.


1 Answers

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

like image 94
Kelvin Edmison Avatar answered Oct 02 '22 12:10

Kelvin Edmison