0

For various reasons, I want to use an environment variable to pass multiple file paths to my script. I hit upon the idea of structuring the variable like PATH is, with colons to separate the entries.

After some research, I came up with the following technique to iterate over the names:

OLD_IFS=$IFS
IFS=":"
# using PATH to demonstrate
for ITEM in $PATH; do
    echo hacking $ITEM which comes from $PATH
done
IFS=$OLD_IFS

It appears to work properly, but I am a bit concerned:

  • Manipulating IFS seems rather awkward and inelegant to me, and not scalable (what if someone else already uses OLD_IFS?).

  • The output shows the colons in PATH replaced with spaces - has the value actually temporarily changed somehow, or is this only a display quirk?

  • This appears not to do globbing if there is e.g. a * in the input variable; but is it properly secure? What other threats might I need to guard against?

  • Then there's the issue of colons. While I understand that any character except / is technically legal in a UNIX directory or file name, I also understand that : is discouraged since it cannot be escaped for the purposes of PATH. However, I still feel like I'd prefer a fully general solution.

Did I miss a better option? Am I supposed to live with these sorts of things in a shell script? I'm much more accustomed to using application development languages like Python, which feel much more "regular" for lack of a better word.

0 Answers0