Newline-separated xargs

Solution 1:

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 

Solution 2:

Something along the lines of

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

should work.


Solution 3:

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!


Solution 4:

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

Tags:

Linux

Bash

Xargs