Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Obviously, syntax preferences in programming languages are very personal, so what someone considers to be simple and beautiful, someone else may consider as complex and ugly.

Nevertheless, in my opinion, the right way to improve the "if ... fi" POSIX shell syntax is not by adding tons of superfluous curly braces, but by removing the redundant square brackets and the redundant semicolon (all of which have historical reasons for their presence).

With those changes, you have the minimal fully-bracketed syntax for a conditional statement, e.g. (if ... then ... elif ... then ... else ... fi).

If you do not like these keywords, that is fine, you should replace them with other keywords or symbols.

For example, I like concise syntax, so I replace those keywords with certain Unicode symbols, i.e. mathematical angular brackets instead of if and fi, section sign instead of elif and else, and left arrow instead of then.

It does not matter what keywords or symbols you like, but both the ease of writing and the ease of reading are improved when each kind of program control structure uses distinct delimiters, not just the same curly braces for everything, and also when there are no redundant delimiters, e.g. in C and derived languages there are a very large number of redundant parentheses, which are needed because the curly braces are optional. Had the curly braces been mandatory in C, the parentheses from if, switch, while, for would have been deleted, which would have lead to easier to read and write programs in the common case, when braces are used.

The final syntax presented has an almost double number of delimiters than needed, presumably because the curly braces are reused for various other purposes in the complete murex shell syntax.

Trying to use too few distinct symbols or keywords is guaranteed to increase the number of redundant delimiters.

Maybe the Algol 68 method of inventing keywords like fi, esac, od, was not the most inspired, but the principle of using different kinds of delimiters for each distinct purpose was, and is, sound.



I agree to an extend and in fact this is what murex already does. Superficially it looks like C (minus semi-colons) but the parentheses are the tokens that replace the ALGOL style then, else and fi keywords. What murex does do is allow those keywords to be optionally included for readability. So all the docs will say

  if { = 1==2 } then {
    err 'math is broken'
  } else {
    out 'math works'
  }
as it's easy to read but really the then and else keywords are just ignored. So the same one liner is perfectly valid

  if{= 1==2} {err 'math is broken'} {out 'math works'}
So you do have the terseness in the interactive shell that you're requesting.

The curly braces do serve an important significance to the parser in murex. Because everything is defined as a function, it means `if` is a builtin and the curly braces are read like quotation marks denoting the start and end of executable parameters (like a subshell but minus the forking). So the `if` command gets the arguments passed like so (pseudo-JSON):

  "ARGS": [
    "if",
    "= 1==2",
    "err 'math is broken'",
    "out 'math works'"
  ]


> if{= 1==2} {err 'math is broken'} {out 'math works'}

So you ended up with kind of like TCL's choices (`then`, `else`, `elseif` are optional; prefix expressions with `=` (`expr` in TCL's case, although it's implied in `if`, `while` conditionals) )


I hadn't noticed until you pointed that out but yes I have!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: