The section 3.4 Using Bracket Expressions of GNU awk manual, reads
To include one of the characters ‘\’, ‘]’, ‘-’, or ‘^’ in a bracket expression, put a ‘\’ in front of it. For example:
[d\]]
matches either ‘d’ or ‘]’. Additionally, if you place ‘]’ right after the opening ‘[’, the closing bracket is treated as one of the characters to be matched.The treatment of ‘\’ in bracket expressions is compatible with other awk implementations and is also mandated by POSIX.
On the other hand, the section Regular Expressions of POSIX awk doesn't list the \]
as having a special meaning. Here are a few experiments with GNU awk (version 5.3.1) and GNU grep (version 3.11) that expose conflicting treatment of the \
in a bracket expression:
$ echo d | awk '/[d\]]/'
d
$ echo d | grep -E '[d\]]'
$ echo ']' | awk '/[d\]]/'
]
$ echo ']' | grep -E '[d\]]'
The question is:
is the GNU awk documentation wrong in claiming that the treatment of \
in a bracket expression in GNU awk is mandated by POSIX, or have I overlooked something?
In other words, does the GNU awk violate the POSIX specification?
[d\]]
matches either ‘d’ or ‘]’, isn't it? It isn't mandated by POSIX; it may match, but doesn't have to. That construct cannot be assured to be portable. – Foreigner