Newline-separated xargs

Is it possible to make xargs use only newline as separator? (in bash on Linux and OS X if that matters)

I know -0 can be used, but it's PITA as not every command supports NUL-delimited output.


Something along the lines of

alias myxargs='perl -p -e "s/\n/\0/;" | xargs -0'
cat nonzerofile | myxargs command

should work.


GNU xargs (default on Linux; install findutils from MacPorts on OS X to get it) supports -d which lets you specify a custom delimiter for input, so you can do

ls *foo | xargs -d '\n' -P4 foo 

With Bash, I generally prefer to avoid xargs for anything the least bit tricky, in favour of while-read loops. For your question, while read -ar LINE; do ...; done does the job (remember to use array syntax with LINE, e.g., ${LINE[@]} for the whole line). This doesn't need any trickery: by default read uses just \n as the line terminator character.

I should post a question on SO about the pros & cons of xargs vs. while-read loops... done!


$ echo "1\n2 3\n4 5 6" | xargs -L1 echo  "#"
# 1
# 2 3
# 4 5 6