Quote:
Originally Posted by
verdepollo
KSH and Bash expansions are guaranteed to create an alphabetically sorted list of names before "feeding" it to cat.
Besides, file name expansion is performed before any redirection occurs.
Not sure about other shells.
Hi verdepollo,
You're correct. A POSIX conforming shell will perform pathname expansions before performing redirections.
Nonetheless, I have seen enough scripts fail because an output file was matched by an input pathname pattern expansion that I advise script writers to avoid the issue. When testing a new script and when a script fails for some reason and needs to be restarted, users frequently forget that they need to remove the output file before restarting the script. Even when it works, a user may want to run the script again the next day, week, or month and the output file can then accidentally become both an input file and an output file, leading to unexpected results. And if it is running on a system being shared with other users, the mistake can affect everyone using that filesystem; not just the user that forgot to remove the output file before restarting the script. Furthermore, when the script completes successfully, users often want to remove the (no longer needed) input files. If the output file matches the pattern that was used to select the input files, it is too easy to accidentally remove not only the input files, but also the output file.
Therefore, I
strongly urge programmers not to create an output pathname that will be matched by the expansion of an input file pattern even though the output file
should not be present when the script starts.