I'm having trouble with an unfortunate mis-feature in bash, illustrated by this function:
trying ()
{
res=0;
echo $res;
cat some_text_file | while read op rpt rec; do
res=1;
echo $res;
done;
echo $res
}
What i had expected was that the last echo $res
would print 1
- however, it prints 0
. The reason is that the loop starting with cat some_text_file | while read op rpt rec; do
runs in a sub-shell, whereas in ksh it doesn't.
A work-around for this simple case would be:
trying ()
{
res=0;
echo $res;
while read op rpt rec; do
res=1;
echo $res;
done < some_text_file;
echo $res
}
However, in most cases it would be more like cat some_file | awk '{...}' | while read ...
, and I'd really like to not create a temporary file just for this; it just ought to work.
Unfortunately I have work with bash on my servers, otherwise it would be ksh everywhere. So, are there any good ways around this?