Next: , Previous: Quadrigraphs, Up: M4 Quotation

8.1.7 Dealing with unbalanced parentheses

One of the pitfalls of portable shell programming is that case statements require unbalanced parentheses (see Limitations of Shell Builtins). With syntax highlighting editors, the presence of unbalanced ‘)’ can interfere with editors that perform syntax highlighting of macro contents based on finding the matching ‘(’. Another concern is how much editing must be done when transferring code snippets between shell scripts and macro definitions. But most importantly, the presence of unbalanced parentheses can introduce expansion bugs.

For an example, here is an underquoted attempt to use the macro my_case, which happens to expand to a portable case statement:

     [case $file_name in
       *.c) echo "C source code";;
     AS_IF(:, my_case)

In the above example, the AS_IF call underquotes its arguments. As a result, the unbalanced ‘)’ generated by the premature expansion of my_case results in expanding AS_IF with a truncated parameter, and the expansion is syntactically invalid:

     if :; then
       case $file_name in
     fi echo "C source code";;

If nothing else, this should emphasize the importance of the quoting arguments to macro calls. On the other hand, there are several variations for defining my_case to be more robust, even when used without proper quoting, each with some benefits and some drawbacks.