What is the reason for the weird syntax of the “case” statement in a bash/zsh script?

后端 未结 4 1921
庸人自扰
庸人自扰 2020-12-29 18:13

Looking from a programmer\'s point of view then shell script is just another programming language, where one has to learn and conform to the rules of the language. However,

4条回答
  •  無奈伤痛
    2020-12-29 18:19

    The closing parenthesis is sometimes used in lists in natural language, like

    1) do this
    2) do that
    

    The reversed keywords were taken from some form of Algol but are in fact a very good idea for interactive use. They clearly demarcate the end of a construct, including if/else.

    For example, with a C-like syntax, after this has been parsed:

    if (condition)
        command here;
    

    is there an else coming or not? rc, a shell from Plan 9 with a more C-like syntax, solves this by providing if not instead of else but it is not pretty.

    With Bourne shell syntax, you'll have either else or fi and there is no need to read additional input.

提交回复
热议问题