for exmaple, using the command
cat foo.txt | xargs -I{} -n 1 -P 1 sh -c "echo {} | echo"
The foo.txt
contains two lines
foo
bar
The above command print nothing.
cat foo.txt | xargs -J % -n 1 sh -c "echo % | bar.sh"
Tricky part is that xargs performs implicit subshell invocation. Here sh invoked explicitly and pipe not becomes the part of parent conveyor
cat foo.bar | wc -l
and cat foo.bar | xargs -J % -n 1 sh -c "echo % | wc -l"
-I
instead of -J
; there is no -J
option to xargs
Jun 1, 2017 at 2:18
-J
isn't defined in POSIX but -I
is and has a different use than GNU's.)
Jun 1, 2017 at 13:59
-J
can take multiple inputs as multiple arguments. -I
is one command per line. Try doing seq 10 | xargs -J % echo %
vs seq 10 | xargs -I % echo %
and you will see. -J % -n 1
is similar to -I
but -n
splits on whitespace. -J % -L 1
is equivalent to -I
.
Mar 14, 2019 at 22:14
If you want to process all the lines of foo.txt you will have to use a loop. Use &
to put the process to background
while read line; do
echo $line | bar.sh &
done < foo.txt
If your input contain spaces temporarily set the internal field separator to the newline
# save the field separator
OLD_IFS=$IFS
# new field separator, the end of line
IFS=$'\n'
for line in $(cat foo.txt) ; do
echo $line | bar.sh &
done
# restore default field separator
IFS=$OLD_IFS