Greg Wooledge (12023-12-15): > On Fri, Dec 15, 2023 at 01:42:14PM +0100, Nicolas George wrote: > > Also, note that file names can also contain newlines in general. The > > only robust delimiter is the NUL character. > > True. In order to be 100% safe, the OP's code would need to look > more like this: > > readarray -d '' fndar < <( > find "$sdir" ... -printf 'stuff\0' | > sort -z --otherflags > ) > > The -d '' option for readarray requires bash 4.4 or higher. If this > script needs to run on bash 4.3 or older, you'd need to use a loop > instead of readarray. > > This may look a bit inscrutable, but the purpose is to ensure that > a NUL delimiter is used at every step. First, find -printf '...\0' > will print a NUL character after each filename-and-stuff. Second, > sort -z uses NUL as its record separator (instead of newline), and > produces sorted output that also uses NUL. Finally, readarray -d '' > uses the NUL character as its record separator. The final result is > an array containing each filename-and-stuff produced by find, in the > order determined by sort, even if some of the filenames contain > newline characters.
It is possible to do it safely in bash plus command-line tools, indeed. But in such a complex case, it is better to use something with a higher-level interface. I am sure File::Find and Version::Compare can let Perl do the same thing in a much safer way. Regards, -- Nicolas George