Skip to content
Lucas Ramage edited this page May 4, 2022 · 6 revisions

Quote parameters to tr to prevent glob expansion.

Problematic code:

tr -cd [:digit:]

Correct code:

tr -cd '[:digit:]'

Rationale:

From the shell's point of view, unquoted [:digit:] is a glob equivalent to [digit:] that matches any single character filename from the group, such as d or t, in the current directory.

If someone starts learning D and creates a directory named d to hold the source code, the glob will be expanded and the script will end up executing tr -cd d instead, which is clearly unintended.

Quoting the argument prevents this, and will pass it correctly as the literal string [:digit:] no matter which files exist in the current directory.

Exceptions

None

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature guerraart8 to find a specific , or see Checks.

Clone this wiki locally