This only exits the subshell caused by the pipeline.
Problematic code:
for i in a b c; do
echo hi | grep -q bye | break
done
Correct code:
for i in a b c; do
echo hi | grep -q bye || break
done
Rationale:
The most common cause of this issue is probably using a single |
when ||
was intended. The reason this message appears, though, is that a construction like this, intended to surface a failure inside of a loop:
for i in a b c; do false | break; done; echo ${PIPESTATUS[@]}
may appear to work:
$ for i in a b c; do false | break; done; echo ${PIPESTATUS[@]}
1 0
What's actually happening, though, becomes clear if we add some echo
s; the entire loop completes, and the break
has no effect.
$ for i in a b c; do echo $i; false | break; done; echo ${PIPESTATUS[@]}
a
b
c
1 0
$ for i in a b c; do false | break; echo $i; done; echo ${PIPESTATUS[@]}
a
b
c
0
Because bash processes pipelines by creating subshells, control statements like break
only take effect in the subshell.
Related resources:
- Contrast with the related, but different, problem in this link.
- Bash Reference Manual: Pipelines, esp.:
Each command in a pipeline is executed in its own subshell.
-
Installation
-
Usage
-
Integrating and extending
Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.
<--- This is a global footer, please don't edit it. --->