Skip to content

Files

Latest commit

 

History

History
30 lines (18 loc) · 785 Bytes

SC2021.md

File metadata and controls

30 lines (18 loc) · 785 Bytes

Pattern: Use of [] around range in tr

Issue: -

Description

Ancient System V tr required brackets around operands, but modern implementations including POSIX, GNU, OS X and *BSD instead treat them as literals.

Unless you want to operate on literal square brackets, don't include them.

Example of incorrect code:

tr -cd '[a-z]'

Example of correct code:

tr -cd 'a-z'

Exceptions

If you do want to replace literal square brackets, reorder the expression (e.g. a-z[] to make it clear that the brackets are not special).

ShellCheck does not warn about correct usage of [..] in character and equivalence classes like [:lower:] and [=e=].

Further Reading