My goal is to automate the creation of multiple directories that end with numerical indexes: dir03 dir04 ... dir09
I tried the following command: mkdir dir0[3-9].
As the result, the shell created a directory named dir0[3-9]
I did try mkdir dir0{3,4,5,6,7,8,9]. It works but it's less convenient.
What are the underlying reasons why mkdir dir0[3-9] doesn't work as intended?
You got the syntax a bit wrong:
[3-9]as used in pattern matching for matching an existing range of values (a range expression in globbing, used in filename expansion and a few other contexts, and also a range expression in regular expressions).{3..9}as used in brace expansion for generating a new range of values (a sequence expression):remove the
echowhen happy with the output.So we use
{3..9}to generate a new range of values:and
[3-9]to match an existing range of values:Note that they look similar but the semantics are different between range expressions in brace expansion and pattern matching, for example in brace expansion
{1..20}expands to the numbers 1 through 20 (because ranges start/end with a potentially multi-digit number or a character) while in a range expression[1-20]expands to just the 3 numbers 1, 2, and 0 (because ranges always start/end with a character, not a string nor a multi-digit number, and so it means the set of the digits 1-2 and the digit 0).That last result is because the range in pattern matching always has to be increasing so
9-2(the range before0in[9-20]) is invalid in a regexp:and in globbing:
but is valid in brace expansion: