How to give a comma-separated list as arguments to the next command
This should equally work as well:
s1 | xargs -d "," -n1 s2
Test case:
printf 1,2,3,4 | xargs -d ',' -n1 echo
Result:
1
2
3
4
If s1
outputs that list followed by a newline character, you'd want to remove it as otherwise the last call would be with 4\n
instead of 4
:
s1 | tr -d '\n' | xargs -d , -n1 s2
If s2
can accept multiple arguments, you could do:
(IFS=,; ./s2 $(./s1))
which temporarily overrides IFS to be a comma, all in a subshell, so that s2
sees the output of s1
broken up by commas. The subshell is a short-hand way to change IFS without saving the previous value or resetting it.
A previous version of this answer was incorrect, probably due to a leftover IFS setting, corrupting the results. Thanks to ilkkachu for pointing out my mistake.
To manually loop over the outputs and provide them to individually to s2
, here demonstrating the saving & resetting of IFS:
oIFS="$IFS"
IFS=,
for output in $(./s1); do ./s2 "$output"; done
IFS="$oIFS"
or run the IFS bits in a subshell as before:
(
IFS=,
for output in $(./s1); do ./s2 "$output"; done
)