The [...]
is a bracket expression. It matches a single character, always, so you can't use [0-100]
as that would just match a single 0
or 1
(in the POSIX locale)
In the zsh
shell, you could use <0-100>
for a numerical range globbing pattern, but that won't work in bash
:
program --files path_to_mydir/mydata_<0-100>.csv
In bash
, you could use a brace expansion instead:
program --files path_to_mydir/mydata_{0..100}.csv
but you have to be aware of the difference between this and a filename globbing pattern. A brace expansion, as the one just above, generates strings, regardless of what filenames are available, while a filename globbing pattern matches existing names. This means that a brace expansion could potentially feed your program filenames that does not exist.
You could use [...]
to match the files with numbers between 0 and 100, but you would have to make it three patterns, one for each length of numbers:
shopt -s nullglob
program --files
path_to_mydir/mydata_[0-9].csv
path_to_mydir/mydata_[1-9][0-9].csv
path_to_mydir/mydata_[1][0][0].csv
The first would match names containing the digits 0
through to 9
, the second would match the names containing 10
through to 99
, and the last would match the name containing 100
.
Would you want to match zero-filled integers:
shopt -s nullglob
program --files
path_to_mydir/mydata_[0][0-9][0-9].csv
path_to_mydir/mydata_[1][0][0].csv
I set the nullglob
shell option in both variations of this code to make sure that any pattern that is not matching any names is removed, and not left unexpanded.
User fra-san noticed that you could use a combination of the brace expansion above with something that would force the shell to trigger a globbing pattern match:
shopt -s nullglob
program --files path_to_mydir/[m]ydata_{0..100}.csv
The inclusion of [m]
in the string (a pattern matching the character m
) would force the shell to treat each of the strings that the brace expansion creates as a separate globbing pattern. Since we're using nullglob
, the patterns that do not correspond to existing names would be removed from the argument list.
Note that this would generate and expand 101 globbing patterns, whereas the other approaches using globbing in this answer uses two or three patterns.
program
and the duplicate usesls
does not make the questions different in essence. – Kusalananda Apr 20 '21 at 12:53