I have a variable,
COMMANDS, that contains a list of newline-separated commands (I know for example in this case that there will never be newlines, but they could be separated by
NUL). For example,
echo "foo bar" tail blah.txt python3 -c 'print(1 + 10)'
I would like to invoke each of these, but using process substitution feed them all as arguments into another program. Continuing with the example, if I wanted to feed into
cat, then I'd expect the following to be run:
cat <(echo "foo bar") <(tail blah.txt) <(python3 -c 'print(1 + 10)')
Is there any way to dynamically do this? The best solution I could come up was to construct this command as a string and give it to
bash -c "cat$(echo "$COMMANDS" | while read -r p; do printf ' <(%s)' "$p"; done)"
But this feels a little brittle (and I question its ability to robustly handle input edge cases like quotes).
For anyone wanting to experiment, here's a quick command to setup what I used as my example:
COMMANDS=$(printf 'echo "foo bar"\ntail blah.txt\npython3 -c "print(1 + 10)"') echo blah > blah.txt