On Sat, Dec 07, 2024 at 06:19:20 -0700, pe...@easthope.ca wrote: > From: Greg Wooledge <g...@wooledge.org> > Date: Fri, 6 Dec 2024 18:44:04 -0500 > > Store the exclusions in an ARRAY, not in a string. Then create a > > second array which contains the spelled-out --exclude=... options. > > Unfamiliar to me & interesting. What benefits outweigh the additional > complexity?
It's LESS complexity! Trying to shove a list of data elements into a string is ALWAYS worse than storing your list of data elements in an array. Consider what happens when one of your data elements has a space in it. What are you going to do then? If you're storing the list in a string with spaces between elements, then you need to add some sort of quoting or escaping *inside* the string. Now it's more complex than if you had simply used the array in the first place. Array assignments in bash also allow you to use free form syntax, and even to add comments. exclusions=( '*.mp3' '*.mp4' # added 2024-12-07 due to ... '*[Rr]ed [Hh]ot*' '*.wav' '*.snd' '*.au' ) Now, you may be thinking "Hey, you said you wouldn't need to add quoting inside the array, but those are quotes!" The difference between this and a string is the quotes in the array assignment are parsed by the shell and removed. hobbit:~$ exclusions=( '*.mp3' '*.mp4' # added 2024-12-07 due to ... '*[Rr]ed [Hh]ot*' '*.wav' '*.snd' '*.au' ) hobbit:~$ declare -p exclusions declare -a exclusions=([0]="*.mp3" [1]="*.mp4" [2]="*[Rr]ed [Hh]ot*" [3]="*.wav" [4]="*.snd" [5]="*.au") Each array element is ready to go. No additional parsing is needed. If you want to do something like "convert this list of globs into a list of --exclude=GLOB options", it's just one parameter expansion. I've shown it already, but here it is again: "${exclusions[@]//#/--exclude=}" In a string, the quotes are literally placed inside the string, and become part of the data payload. You have to write your *own* parsing code to separate the data elements and remove the quotes. The shell won't be able to do it for you. hobbit:~$ unset exclusions hobbit:~$ exclusions='*.mp3 *.mp4 "*[Rr]ed [Hh]ot*" *.wav *.snd *.au' hobbit:~$ declare -p exclusions declare -- exclusions="*.mp3 *.mp4 \"*[Rr]ed [Hh]ot*\" *.wav *.snd *.au" That's so much harder to deal with. You can't do anything with it until you parse it back out into a list. Essentially you'll be converting the string into an array, and then using the array operations that I've already shown. It's so much easier just to use an array in the first place.