I'm teaching myself bash, and using Learning the bash Shell to do so.
I noticed in the flow control that we define an if/else statement as so:
if [ statement_is_true ]; then
# do stuff here
elif [ some_other_statement]; then
# other thing
else
# if all else fails
fi
and a case statement as such:
case $expr in
case1 )
;;
case2 )
;;
esac
but a for loop might be defined as
for i in $list;
do
# something cool with the various $i
done
and a while loop as
while [ condition ]; do
# something that loops
done
Why is the end of the for
, while
, and until
loops denoted with done
rather than rof
, elihw
, and litnu
, respectively, like the if/fi
and case/esac
construct? (Alternatively, why is it if/fi
instead of if/done
?
I suspect that it's just readability, because litnu
is much harder to read/pronounce than esac
. See, for example, this book, where "end
scores over nigeb
for obvious reasons."
fi
and esac
are easy to pronounce and distinguish from other code. The do...done
structure also provides a consistent syntax within loop constructs, which is arguably more sensible than consistent syntax across all control flow statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With