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)
(...)
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.
!
: 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:
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).
What is the advantage of making something an operator instead of a reserved word?
What is the advantage of making something a reserved word instead of an operator?
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.
a
a filename in<a
, but a variable inread a
?". – Kusalananda Jan 26 '17 at 11:28