Actually, the above loop should have no problem processing filenames with spaces.
The
*.mkv globbing pattern expands to a list of all .mkv filenames in the directory, however they're named, and the loop will read each one in turn. Since the shell does the glob expansion step
after word-splitting has been completed, each expanded name is treated as a separate argument. Therefore it's safe to use glob input in a
for loop.
http://mywiki.wooledge.org/glob
Note that other kinds of input string aren't safe, such as with the output of a command or variable substitution, because parameter expansions happen before word-splitting. The substituted strings will be broken up into individual words before the loop sees them. In these cases, you should use a
while+read loop.
http://mywiki.wooledge.org/DontReadLinesWithFor
http://mywiki.wooledge.org/BashFAQ/001
The only other place where spaces would be a problem is when the variable is expanded later on in the mkvmerge command, and for the same reason. But since double-quoting variables protects the expanded string from word-splitting, we're safe here too.
For more on how the shell handles arguments and whitespace, see here:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
Edit: Incidentally, case matching can be handled simply by adding
shopt -s nocaseglob to the script, or by using
*.[mM][kK][vV] as your glob pattern.