Bash’s choice: operator vs reserved word

bash

In Bash, I notice that there are some examples of choosing something as an operator or a reserved word:
(all quotes are from Bash Manual)

  1. (...) and {...}: They are both used for creating a list of commands.

    But the first one is operators, while the second one is reserved words:

    In addition to the creation of a subshell, there is a subtle
    difference between these two constructs due to historical reasons.
    The braces are reserved words, so they must be separated from the
    list by blanks or other shell metacharacters. The parentheses are
    operators, and are recognized as separate tokens by the shell even
    if they are not separated from the list by whitespace.

  2. !: It means logical negation.

    It is a reserved word when it precedes a pipeline:

    If the reserved word ‘!’ precedes the pipeline, the exit status is the logical
    negation of the exit status as described above.

    It is an operator in three places:

    • in shell arithmetic expressions,
    • in conditional expressions within [...]
    • in conditional expressions within [[...]].

My questions are:

  1. Why are some of them reserved words, and the others operators, despite that they are used for the same meaning?

    My intention isn't to learn about the design history of Bash, or why Bash is designed as it is today, (so it is fine to skip this question), although that helps. My intention is to understand what operators can do while reserved words can't, and vice versa (see the following two questions).

  2. What is the advantage of making something an operator instead of a reserved word?

  3. What is the advantage of making something a reserved word instead of an operator?

  4. Are there other similar examples?

    My intention isn't just to collect the examples, but to derive some principles from them (see the above two questions), so that I can better understand the specifics and become less inclined to be confused and make mistakes. So it is fine to skip this question.

Best Answer

Firstly, let's differentiate these two things, "operator" and "reserved word." They are not even at the same level of abstraction, they are entirely different things. Start with standard English-language definitions*:

operator: a symbol or function denoting an operation.

(Note that "symbol" does not imply "single character"; a word is a symbol, too.)

operation: a process in which a number, quantity, expression, etc., is altered or manipulated according to formal rules, such as those of addition, multiplication, and differentiation.

Hopefully clear? Make up a few examples of your own; it will help.

Then we have "reserved word," for which we really only need to look up "reserved":

reserved: kept specially for a particular purpose or person

In this case we mean words that were kept for a particular purpose by the shell designers, and may not be used for other purposes (at least, not without escaping them).


Note that these terms do not describe disjoint sets of things. That is, there are reserved words which denote the alteration or manipulation of expressions (or commands) according to formal rules.

In actual fact, ! as a "reserved word" preceding a pipeline is in fact an operator on the exit status of that pipeline, just by standard English definitions.

The issue is purely one of semantics, and the difference is not as important as the authors of that particular manual seem to believe. The application of these labels comes after the fact of the design and the labels are used to describe the concrete behavior embedded in the design; neither item ("reserved word" or "operator") has an inherent advantage over the other. The labels may have advantages in some cases, but frankly they actually just mean different things. They are just two different terms.


Specific programmatic meanings:

From a cursory look over that particular manual, the key terms for this question actually appear to be:

  • token
  • word
  • metacharacter
  • operator
  • control operator
  • reserved word

In that sequence.

My takeaway:

A word is something that must be separated from other words by placement of metacharacters. An operator itself consists of "word separators" (metacharacters) and so does not need to be specifically separated from words.


Thus the entirety of the important difference between {...} and (...) could have been conveyed in the quotation you cite without even using the terms that prompted this question. Here is a rewording:

In addition to the creation of a subshell, there is a subtle difference between these two constructs due to historical reasons. The braces must be separated from the list by blanks or other shell metacharacters. The parentheses are recognized as separate tokens by the shell even if they are not separated from the list by whitespace.

The entire meaning is there. This defines the difference between "operator" and "reserved word." There is no other difference.


*Definitions are taken from New Oxford American Dictionary.