Skip to content

Files

Latest commit

 

History

History
42 lines (27 loc) · 1.48 KB

SC2102.md

File metadata and controls

42 lines (27 loc) · 1.48 KB

Pattern: Glob range expression with multiples of the same character

Issue: -

Description

Range expressions can only be used to match a single character in a given set, so [ab] and [abba] will both match the same thing: either one a or one b.

Having multiple of the same character often means you're trying to match more than one character, such as in the problematic example where someone tried to match any number from 100 to 999. Instead, it matches a single digit just like [0-9].txt, and specifies 0, 1 and 9 multiple times.

In Bash, most uses can be rewritten using extglob and/or brace expansion. For example:

cat *.[dev,prod,test].conf   # Doesn't work
cat *.{dev,prod,test}.conf   # Works in bash
cat *.@(dev|prod|test).conf  # Works in bash with `shopt -s extglob`

In POSIX sh, you may have to write multiple globs, one after the other:

cat *.dev.conf *.prod.conf *.test.conf

Example of incorrect code:

echo [100-999].txt

Example of correct code:

echo [1-9][0-9][0-9].txt

Exceptions

There is currently a bug in which a range expression whose contents is a variable gets parsed verbatim, e.g. [$foo]. In this case, either ignore the warning or make the square brackets part of the variable contents instead.

Further Reading