Why is there a question of order in this class of escaped characters in sed?

This works as expected:

>echo -= | sed 's/[\=\-]//g'


But this is not the case:

>echo -= | sed 's/[\-\=]//g'




source to share

1 answer

The character class -

can be used to create a range (for example, [A-Z]

that is the entire character from A

to Z

, not three characters A

, -

and Z


That way, when you write [\-\=]

, that is treated as a range from \

to \=

(I don't think escaping =

makes sense here).

But when you write the [\=\-]

interpretation of the range is not possible because the trailing ]

class cannot be the end of the range.

Likewise, if you wrote [-\=]

, you would not have an interpretation of the range of this class.

As I said, while I don't think escaping any of these characters makes sense, it is actually what caused this problem as it [-=]

works the same and doesn't have a range issue.

Likewise, using ^

a character class at the beginning negates the class, so if you want a literal ^

in the class, it doesn't have to be the first character in the class.



All Articles