Skip to content

grep causes false positives with SC2016, '\$123' '\$(ab|cd)' #3217

Open
@oittaa

Description

@oittaa

The documentation states https://www.shellcheck.net/wiki/SC2016:

ShellCheck tries to increase the signal-to-noise ratio of this warning by ignoring certain well known commands that frequently expect literal dollar signs, such as sh and perl.

It would be nice if it tried to detect at least some common grep patterns.

A simple test case would be checking if there's a literal $123.

#!/bin/sh
head -n 1 /dev/urandom | grep -E '\$123'

-> SC2016

There are workarounds, but they are quite ugly. Triple backslash within double quotes gets very confusing.

#!/bin/sh
head -n 1 /dev/urandom | grep -E '\$[1]23'
head -n 1 /dev/urandom | grep -E "\\\$123"

This passes!

Other common error case goes like this:

#!/bin/sh
head -n 1 /dev/urandom | grep -E '\$(ab|cd)'

-> SC2016

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions