${dir##*/}
Changing global vars in sub shell won't affect it's value in global context, like:
global_var=asdf
foo()
{
global_var=$(echo 4321)
}
echo something | grep some_pattern | \
while read line
do
foo
echo $global_var
done
echo $global_var
# outputs:
# 4321
# asdf
Finally I resigned to save that variable to a temporary file.