Thanks for posting. I find the echo part and extra use of variable is a little bit flaky. Here is a modified version. But I am not 100% sure if its doing what your script is doing.
I skipped the extra variable and echo and grep, by comparing its content with ${*}, which is similar to ${@}, but won't separate each argument and create a single string instead. The =~ /$ is a regex comparison, which Bash supports native. Then I am using ${@} for the call, which separates each argument. Maybe this could be done with ${*} instead. I'm not sure which of them is the correct one for this case. At least it seems filenames with spaces work. Otherwise, not claiming it would be better. Just giving some food for thoughts.
#!/usr/bin/bash
if [[ "${*}" =~ /$ ]]; then
xargs -rd '\n' -I {} "${@}"{}
else
xargs -rd '\n' -I {} "${@}" {}
fi