Skip to content
maxnikulin edited this page Apr 14, 2023 · 9 revisions

In POSIX sh, ^ in place of ! in glob bracket expressions is undefined.

Problematic code:

echo foo-[^0]*.jpg

Correct code:

echo foo-[!0]*.jpg

Rationale:

[^c] is frequently used in most regular expression variants to mean "any character except c". Ksh and Bash adopted it for globs as well.

However, strictly speaking, the only range complement syntax guaranteed to be supported across shells is [!c]. Dash used to support [^c] when compiled with fnmatch and glob from glibc, but it was considered as a bug and fixed in version 0.5.12. Busybox has a similar bug 14516.

Exceptions:

If you only intend to target shells that supports this feature, you can change the shebang to a shell that guarantees support, or ignore this warning. Or just rewrite it to be on the technically correct side.

Related resources:

Clone this wiki locally