-o pipefail makes while read loop ugly
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
mksh |
Invalid
|
Medium
|
Thorsten Glaser |
Bug Description
tc.sh:
set -x
set -e
cat >tc.in <<\EOF
one eins
two zwei
three drei
EOF
while IFS=' ' read a b; do
# $a = t* and it works
[[ $a = o* ]] && echo $b
done <tc.in | sort -u >tc.out
echo after the loop
cat tc.out
echo and out
This works with t* both with set ±o pipefail, and with t* in ksh93, but t* in mksh with set -o pipefail makes it error out *after* the “while” because the last comparison error’d out.
Workarounds include negating the comparison…
[[ $a != o* ]] || echo $b
… so that the loop always has $?=0, or:
while :; do
IFS=' ' read a b || break
Interestingly enough, removing the “| sort -u” also makes it work.
This is, unfortunately, by design.
I’ve committed a manpage change (in CAVEATS) with a rationale, though.