bash: "undefined variable" and pipe

Hi,
haven't found anything about this through searching, so may be a new topic:
when doing this:

set -o nounset
set -o errexit
find . -name "*.lib" | while read library; do
echo ${libary}
done
echo "after while"

I expect the script to exit within the while loop (because of nounset and the typo libary!=library). Actually, the script continues, probably because the pipe is executed in a subshell. But shouldn't the undefined variable error propagate up to the calling shell and trigger a non-zero exit?
Is there an option or way to force this?
Thanks in advance.

[bash]

$ trap "echo I\'m out..." exit
$ set -o nounset -o errexit
$ find ./t -name "*.lib"|while read library; do echo $libary; done
bash: libary: unbound variable
$ while read library; do echo $libary; done< <(find ./t -name "*.lib")
bash: libary: unbound variable
I'm out...

Thanks for your answer, radoulov, but I'm not sure that's the solution, because the trap doesn't catch within the pipe, only when the script ends:

my script is called "b":
trap "echo I\'m out..." exit
set -o nounset -o errexit
find ./t -name "*.lib"|while read library; do echo $libary; done
echo the end

$ sh b
b: line 3: libary: unbound variable
the end
I'm out...

Anyway this would trap *all* exits, the good ones as well as the bad ones, which is not what I'm after. I want the script "b" to exit with a non-zero return value as soon as it's encountering the unbound variable, even befor the while loop is completed.

The trap was only for the visual effect
the point was the process subsitution,
consider this:

% cat s
#!/bin/bash -ue

find ./t -name "*.lib"|while read library;do
  echo $libary
done

echo after the first loop # you'll see it

while read library;do
  echo $libary
done< <(find ./t -name "*.lib")

echo after the second one # you won't see it

ubuntux% ./s
./s: line 4: libary: unbound variable
after the first loop
./s: line 10: libary: unbound variable

Hi,

Try this script. This may help you.

set -o nounset
#set -o errexit
find . -name "*.txt" | while read library; do
echo ${libary}
done
RC=$?  #return status of the previous command
if [ $RC -ne 0 ]
then
        exit
fi
echo "after while"

Regards,
Chella

Thanks chella, but your example only warns when you expect an error. My point was that an unsuspected "unbound variable" could get unnoticed.

radoulov, that works. I got a bit confused by the different behaviours of "./s" and "sh s" (the latter gives a syntax error).
Although, as I said, I was looking more for a setting that would raise an error with the existing code, rather than using a different construct, because when I know where to look, I can check unbound variables easily by hand.
Anyway, it probably is a feature in bash (not a bug) so I just have to be watchful.

Thank you very much again!

I think you should understand the difference between sh command and
./command
(in the first case the shebang is only a comment):
Try changing the script like this:

% cat s
#!/bin/bash -ue
find ./t -name "*.lib"|while read correct;do
  echo $wrong
  echo $correct
done||exit

echo after the loop

% ./s
./s: line 3: wrong: unbound variable
% sh s

./t/1.lib
after the loop
% sh -ue s
s: 5: wrong: parameter not set

Consider this:

% cat s
#!/bin/bash -ue

find ./t -name "*.lib"|while read correct;do
  echo $wrong
  echo $correct
done

echo after the loop

% sh s

./t/1.lib
after the loop
% sh -uevx s
#!/bin/bash -ue

find ./t -name "*.lib"|while read correct;do
  echo $wrong
  echo $correct
done
+ find ./t -name *.lib
+ read correct
s: 1: wrong: parameter not set