mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2126 (markdown)
@@ -1,4 +1,4 @@
|
||||
## Consider using grep -c instead of grep|wc.
|
||||
## Consider using `grep -c` instead of `grep | wc`
|
||||
|
||||
### Problematic code:
|
||||
|
||||
@@ -38,7 +38,7 @@ if grep -q pattern file; then
|
||||
fi
|
||||
```
|
||||
|
||||
Also note that in `foo | grep bar | wc -l`, wc will mask the exit code of grep by default (i.e. without `set -o pipefail`), and always return success. If replacing with `foo | grep -c bar`, grep will exit non-zero when the count is 0. This is convenient for conditional statements but may require handling when used with `set -e`.
|
||||
Also note that in `foo | grep bar | wc -l`, `wc` will mask the exit code of `grep` by default (i.e. without `set -o pipefail`), and always return success. If replacing with `foo | grep -c bar`, `grep` will exit non-zero when there are no matches. This is generally desirable (see above), but may require handling when used with `set -e`.
|
||||
|
||||
### Exceptions
|
||||
|
||||
|
Reference in New Issue
Block a user