Some bits of grep usage are where I disagree with Shellcheck

April 17, 2020

I have come to very much like shellcheck and now automatically use it on any shell script I'm writing or revising (at least any scripts for work; personal ones I'm sometimes lazier with). Although some of its issues are a little bit picky, using it is good for me so I go along with them. Well, with one exception, because I cannot go along with Shellcheck's views on using egrep and fgrep.

If you've used Shellcheck on scripts, especially older ones, you have probably run into SC2196 and SC2197:

egrep is non-standard and deprecated. Use grep -E instead.
fgrep is non-standard and deprecated. Use grep -F instead.

Sorry, nope. Both egrep and fgrep are long standing programs and names for these versions of grep, and they will be present on any sane Unix until the end of time (and I don't mean 2038, the end of the 32-bit Unix epoch). I have a visceral and probably irrational reaction to the idea of using the POSIXism of 'grep -E' and 'grep -F' for them, and I have no intention of switching my scripts away from using fgrep and egrep.

(And grep already has enough flags to keep track of, including important ones for searching test files in GNU Grep.)

Fortunately modern versions of Shellcheck (including the one in Ubuntu 18.04) can disable these warnings for your entire script, as covered in the wiki's page on ignoring errors. Just put a line in the comment at the start of your script to turn them off:

#!/bin/sh
[...]
# Disable warnings about use of egrep and fgrep
# shellcheck disable=SC2196,SC2197

(I don't want to use a $HOME/.shellcheckrc for this, because I want our scripts to Shellcheck cleanly regardless of who runs Shellcheck.)


Comments on this page:

By George Sexton at 2021-05-25 10:33:56:

It's not a function of shellcheck.

man egrep

"In addition, the variant programs egrep and fgrep are the same as grep -E and grep -F, respectively. These variants are deprecated, but are provided for backward compatibility."

shellcheck is merely reporting it as deprecated because the originators report it as deprecated.

By cks at 2021-05-25 15:43:15:

As the POSIX specification for grep says in the rational:

The old egrep and fgrep commands are likely to be supported for many years to come as implementation extensions, allowing historical applications to operate unmodified.

The nominal deprecation of fgrep and egrep is a pleasant fiction. They're never going away on any Unix that people want to be useful and broadly accepted. Shellcheck's report is technically correct but not practically so.

Written on 17 April 2020.
« Some ways that servers make their disks not hot-swappable
We've disabled eBPF for normal users on our Linux machines »

Page tools: View Source, View Normal.
Search:
Login: Password:

Last modified: Fri Apr 17 00:48:05 2020
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.