I have some command foo
that produces a list of files (one per line).
I'd like to safely use that in command substitution; e.g. git checkout ... -- $(foo)
. Since filenames can have special characters and spaces, I'd like to avoid any word-splitting or other issues.
What's a good way to go about this? Keep in mind I anticipate needing to do this quite a bit, so it'd be nice to have a solution that's not janky (interpret however you like.)
IFS
can be set to respect spaces when making arrays:
IFS=$'\r\n' eval 'FILES=($(foo))'
Finally, this can be used in the script as needed by doing:
git checkout ... -- "${FILES[@]}"