Here goes the riddle:
If I do:
touch file{1,2,3}
It creates file1, file2, file3
And if I do
rm file[1-3]
It deletes them.
but if I do
touch file[1-3]
it creates:
file[1-3]
Why?
bashsyntax
Here goes the riddle:
If I do:
touch file{1,2,3}
It creates file1, file2, file3
And if I do
rm file[1-3]
It deletes them.
but if I do
touch file[1-3]
it creates:
file[1-3]
Why?
Best Answer
If you took the trouble of reading the manpage instead of making riddles:
file[1-3]
expands into files namedfile1
,file2
,file3
. Filename expansion happens only if matching files exist. If not, the pattern is left as-is. Therefore, with files namedfile1
,file2
,file3
,file[1-3]
expands tofile1 file2 file3
. Without these files, it doesn't expand, and remains asfile[1-3]
. With{...}
, the filenames don't have to exist, sofile{1..3}
expands tofile1 file2 file3
irrespective of the files being present or absent.