Search code examples
linuxglob

Whats the difference between [01], [0-1] and [0,1] in linux globbing?


Title, is there a reason to use one and not the other in X case?

[root@localhost ~]# ls 192.168.[0,1].1
192.168.0.1  192.168.1.1
[root@localhost ~]# ls 192.168.[01].1
192.168.0.1  192.168.1.1
[root@localhost ~]# ls 192.168.[1].1
192.168.1.1
[root@localhost ~]# ls 192.168.[01].1
192.168.0.1  192.168.1.1
[root@localhost ~]# ls 192.168.[0-1].1
192.168.0.1  192.168.1.1
[root@localhost ~]# ls 192.168.[10].1
192.168.0.1  192.168.1.1
[root@localhost ~]# ls 192.168.[01].1
192.168.0.1  192.168.1.1

Solution

  • The expression [...] is part of the filename expansion set. The three examples you give are all different, but in some cases two of them can be equivalent:

    • [01]: this matches the character 0 or 1
    • [0,1]: this matches the character 0 or 1 or ,
    • [0-1]: this is a range expression, any character that falls between those two characters (0 and 1) will be matched. This depends on the current locale and the values of LC_COLLATE and LC_ALL. If LC_ALL=C, the range [0-1] and the set [01] will be equivalent.

    This pattern is used in a filename expansion, this implies that if you would have a file named 192.168.,.1 it would have been matched with [0,1].