mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2143 (markdown)
24
SC2143.md
Normal file
24
SC2143.md
Normal file
@@ -0,0 +1,24 @@
|
||||
## Instead of [ -n $(foo | grep bar) ], use foo | grep -q bar .
|
||||
### Problematic code:
|
||||
|
||||
if [ "$(find . | grep 'IMG[0-9]')" ]
|
||||
then
|
||||
echo "Images found"
|
||||
fi
|
||||
|
||||
### Correct code:
|
||||
|
||||
if find . | grep -q 'IMG[0-9]'
|
||||
then
|
||||
echo "Images found"
|
||||
fi
|
||||
|
||||
### Rationale:
|
||||
|
||||
The problematic code has to iterate the entire directory and read all matching lines into memory before making a decision.
|
||||
|
||||
The correct code is cleaner and stops at the first matching line, avoiding both iterating the rest of the directory and reading data into memory.
|
||||
|
||||
### Contraindications
|
||||
|
||||
None.
|
Reference in New Issue
Block a user