Skip to content

no-ls-grep

Shellsuspiciousno fixdefault: warnshell/shellcheck

Reports a pipeline from ls into grep.

ls output is for people and grep matches lines, so a file name with a newline is split and one that looks like a line of output is matched wrongly. A glob or find selects names without parsing text.

To fix, use a glob (ls *foo*, or for f in *foo*) or find.

Reported
...
trap 'rm $tmp' EXIT
trap "echo bye" EXIT
ls | grep txt
...
[ -n x ] && (a || b)
a && b
a || b && c
ls -l | grep txt | wc -l
...
shell:
lint:
rules:
no-ls-grep: warn # off, warn (default), error

Enabled by the presets: shell/shellcheck.

Suppress one line with sudsy-disable-next-line no-ls-grep in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
shellcheck SC2010 full