-
Notifications
You must be signed in to change notification settings - Fork 1.8k
SC2049
Joachim Ansorg edited this page Nov 12, 2021
·
2 revisions
[[ $file =~ *.txt ]]
[[ $file = *.txt ]]
You are using =~
to match against a regex -- specifically a Extended Regular Expression (ERE) -- but the right-hand side looks more like a glob:
-
It may have a leading
*
, like in*.txt
- In a glob, this matches strings ending in
.txt
, likereadme.txt
but notfoo.sh
- In an ERE, this matches a literal asterisk, followed by any character, and then
txt
, such as*itxt
but nottest.txt
- In a glob, this matches strings ending in
-
It may be a single letter followed by a
*
, like ins*
.- In a glob, this matches strings starting with
s
, such asshell
andset
. - In an ERE, this matches zero or more
s
s, such asdog
(because it does in fact contain zero or mores
's)
- In a glob, this matches strings starting with
Please ensure that the pattern is correct as an ERE, or switch to glob matching if that's what you intended.
This is similar to SC2063, where grep "*foo*"
produces an equivalent warning.
If you are aware of the difference, you can ignore this message, but this warning is not emitted for the more probable EREs \*.txt
, \.txt$
, ^s
or s+
, so it should rarely be necessary.
- Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!