tr: trying to find the right incantation of a character class...

Cristian Ionescu-Idbohrn cristian.ionescu-idbohrn at axis.com
Tue Aug 29 17:23:39 UTC 2017


...to do this:

$ echo 'hello world-1' | tr -d '[ -]'

that is, remove all SPACES (0x20) and dashes (0x2D) in input?  I get 
mixed results with both various versions of busybox but also with the 
coreutils `tr' and the characters order placed in the character class.

`man 7 regex' is telling us:

	To  include a literal '-', make it the first or last 
	character, or the second end-point of a range.
	To use a literal '-' as the first endpoint of a range, enclose 
	it in "[."  and  ".]" to make it a collating element (see 
	below).  To use a literal '-' as the first endpoint of a 
	range, enclose  it  in "[."  and  ".]" to make it a 
	collating element (see below).  With the exception of these 
	and some combinations using '[' (see next paragraphs), all 
	other special  characters, including '\', lose their special 
	significance within a bracket expression.

and so on.

So, I could list a few unintuitive character classes that would 
produce unintuitive results, with both coreutils `tr' and busybox 
`tr'.  What is the correct incantation that would always work as 
expected?


Cheers,

-- 
Cristian


More information about the busybox mailing list