Bash is simply insane
What do you think the following scripti will print?
foo() { true | while true; do false rc=$? if [ $rc -eq 1 ]; then return 1 fi done echo $? return 0 } foo || echo "Failed foo"
Run it and see. I suspect everyone but the script gurus out there will be surprised.
What about this script then?
bar () { local rc=0 true | while true; do false rc=$? if [ $rc -eq 1 ]; then return 1 fi done echo $? echo $rc return 0 } bar
Surprised again?
I guess this means that scoping in bash is somewhat more complicated then I would have ever guessed.
- The script is somewhat artificial, who would ever use the construct
true | while ...? I’ve used this just to show the point while keeping the examples short. Feel free to replace that part with something more useful, likecat myfile | while read ....[back]