A loop counts errors, but after the loop the count is always 0.
IntermediateTroubleshootingTroubleshooting: quoting, expansion and tool-input bugs
Answer
Likely causes → The loop is on the right side of a pipe (grep ERROR app.log | while read -r l; do count=$((count+1)); done), so it runs in a subshell and its variables vanish when it ends.
Diagnose → echo "inside: $count" at the end of the loop body shows the right number; after the loop it is 0.
Fix →
count=0
while IFS= read -r _; do count=$((count + 1)); done < <(grep ERROR app.log)
Or shopt -s lastpipe in scripts, or simply count=$(grep -c ERROR app.log || true).
Prevent → Feed loops with redirection or process substitution, not pipes, when they set variables.
More on Troubleshooting: quoting, expansion and tool-input bugs
`bash: [: too many arguments`Beginner`bash: [: =: unary operator expected`BeginnerA disk check fails with `[: 93%: integer expression expected`Beginner`rm $file` fails with `rm: cannot remove 'monthly': No such file or directory` and `rm: cannot remove 'report.csv'`.BeginnerA cleanup script logs `cp: cannot stat '/var/log/app/*.log': No such file or directory` on quiet days.Beginner`echo "start" > $logfile` fails with `bash: $logfile: ambiguous redirect`Intermediate