How come FILE_FOUND is 0 at the end of this bugger :
FILE_FOUND=0
touch /tmp/$$.txt
ls -1 /tmp/$$.* 2>/dev/null | while read item; do
FILE_FOUND=1
echo "FILE_FOUND = $FILE_FOUND"
done
echo "FILE_FOUND = $FILE_FOUND"
rm -f /tmp/$$.txt 2>/dev/null
??!!
On Unix FILE_FOUND stays at 1 (as it should), but on Linux (RedHat, Cygwin, ..) it jumps back to 0!!
Is it a Linux shell feature, not a bug? 🙂
Please help.
common issue which is caused because you’re piping into the while, which is therefore run in a subshell, which can’t pass environment variables back to its parent. I’m guessing that “unix” is different in this regard as you’re running a different shell there (ksh?)
piping to the while loop may not be required. could you use this idiom instead?
If you must use a subshell, then you’ll have to do something external to the processes like: