Redirect to stdin instead of argument when using xargs [closed]

Solution 1:

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

Solution 2:

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