Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are bash's flow control constructs inconsistent? [duplicate]

Tags:

bash

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?

like image 338
Dang Khoa Avatar asked Jun 03 '14 02:06

Dang Khoa


1 Answers

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.

like image 62
Patrick Collins Avatar answered Oct 18 '22 19:10

Patrick Collins