Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 已超出表达式递归级别_Bash_Arithmetic Expressions - Fatal编程技术网

Bash 已超出表达式递归级别

Bash 已超出表达式递归级别,bash,arithmetic-expressions,Bash,Arithmetic Expressions,不知道以下示例中出现错误的原因: $ a=1; (( a > 0 )) && echo y || echo n y $ a=x; (( a > 0 )) && echo y || echo n n $ a=a; (( a > 0 )) && echo y || echo n -bash: ((: a: expression recursion level exceeded (error token is "a") n 这种行为是

不知道以下示例中出现错误的原因:

$ a=1; (( a > 0 )) && echo y || echo n
y
$ a=x; (( a > 0 )) && echo y || echo n
n
$ a=a; (( a > 0 )) && echo y || echo n
-bash: ((: a: expression recursion level exceeded (error token is "a")
n
这种行为是因为
declare-i
将赋值的RHS放在算术上下文中。在算术上下文中,bash递归地将变量名取消引用到它们的值。如果名称取消对自身的引用,则会出现无限递归

为了进一步澄清,只有在对变量名称设置integer属性之前,将该变量分配给与该变量名称相同的字符串时,才会出现这种行为

$ unset a
$ declare -i a
$ a=a
( This is fine, $a dereferences to 0. )
$ unset a
$ a=a
$ declare -i a
$ a=a
-bash: ((: a: expression recursion level exceeded (error token is "a")
这就是为什么这种情况很少发生。如果在已经处于算术上下文中的情况下执行赋值,则右侧不能解析为整数以外的任何内容。不可能发生递归。所以

  • (())
    内执行所有操作。(你也可以在那里做作业。)
  • 首先使用
    declare-i
    ;不要混用

  • 当在算术表达式中使用变量,但值不是数字时,shell将其视为另一个要求值的表达式。因此,如果该值是一个变量名,它将获取该变量的值并使用它。但在本例中,它指向自身。因此,要评估
    a
    ,它必须评估
    a
    ,这会不断重复。

    我很确定
    a=a
    既不是你的意思,也不是你想要的。谢谢你的解释。
    $ unset a
    $ declare -i a
    $ a=a
    ( This is fine, $a dereferences to 0. )
    $ unset a
    $ a=a
    $ declare -i a
    $ a=a
    -bash: ((: a: expression recursion level exceeded (error token is "a")