According to this post, [
is a builtin and [[
is a keyword. Since they can be regarded as command, everything after [
and [[
is regarded as command arguments and should be separated by spaces, until they meet final argument ]
and ]]
But what is the type of ((
?
type ((
Run Code Online (Sandbox Code Playgroud)
will give an error:
bash: syntax error near unexpected token `('.
Run Code Online (Sandbox Code Playgroud)
I found that spaces is not relevant inside (( ))
.
echo $((1+1))
echo $(( 1+1))
echo $(( 1+1 ))
echo $(( 1+ 1))
echo $(( 1 + 1 ))
Run Code Online (Sandbox Code Playgroud)
are all the same
summary
according to POSIX standard doc: Grouping Commands section
If a character sequence beginning with "((" would be parsed by the shell as an arithmetic expansion if preceded by a '$', shells which implement an extension whereby "((expression))" is evaluated as an arithmetic expression may treat the "((" as introducing as an arithmetic evaluation instead of a grouping command. A conforming application shall ensure that it separates the two leading '(' characters with white space to prevent the shell from performing an arithmetic evaluation.
And the arithmetic evaluation inside ((...))
is conformed with C style. So space is not relevant inside ((...))
((
不是一个词。[
与and不同[[
,它不是构成命令的单词列表的一部分。最好的方式是将其视为标点符号。实际上,它是两个标点符号,根据上下文,它们可能会也可能不会组合在一个标记中。在这方面,它类似于>&
和 之类的东西。||
shell 的词法分析器/解析器以特殊方式处理标点符号。
((
是运算符的一半,另一半是))
。它们一起构成算术表达式运算符,其语法为 \xe2\x80\x9coutfix\xe2\x80\x9d。bash 等 shell 语言的语法结构并不完全遵循传统编程语言的语法(例如,词法分析和语法分析之间存在很强的联系,因为标点符号的含义取决于语法上下文),因此并不总是很合适的词。
type
只能为您提供有关可以出现在简单命令中的单词和关键字的信息,而不是有关 shell 的词法分析器/解析器如何工作的信息。