3 ms·
I much prefer ksh myself, in particular for scripting. Fun fact about how bash is broken: export a=0 cat $file | while read line do a=$(expr $a + 1)
by johnpmayer 13y ago
I much prefer ksh myself, in particular for scripting. Fun fact about how bash is broken:
export a=0
cat $file | while read line
do
a=$(expr $a + 1)
done
Bash will fork the while loop as a separate process, inheriting (copying) its own environment, and the variable will only update locally, not in the parent process.
- McUsr 13y agoI thought every shell that is sh compatible would fork a while, and a for loop, as a matter of fact any loop into a sepearate process. This comment however reminds me of some behaviour of bash 2.03 or earlier, where this behaviour was flawed, if you suddenly changed execution path during a recursion in a shell script. Sorry I can't describe it better, and that bash version, that was before 2.03 is long gone now!
- zwp 13y agoIsn't the issue the pipe, not while? export a=0 while read line ; do a=$(expr $a + 1) done < $file echo $a (Some old shells fork on < but bash isn't one of them).
- McUsr 13y agoI think the pipe was the issue. Nice to know that bash doesn't fork loops that are being used with redirection, and not piping!
- UNIXgod 13y agosave a kitten =)