bash - 如何声明本地整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11187443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:35:24  来源:igfitidea点击:

bash - How to declare a local integer?

bash

提问by helpermethod

In Bash, how do I declare a local integer variable, i.e. something like:

在 Bash 中,我如何声明一个局部整数变量,例如:

func() {
  local ((number = 0)) # I know this does not work
  local declare -i number=0 # this doesn't work either

  # other statements, possibly modifying number
}

Somewhere I saw local -i number=0being used, but this doesn't look very portable.

我看到有人在某处local -i number=0使用,但这看起来不太便携。

回答by ecatmur

Per http://www.gnu.org/software/bash/manual/bashref.html#Bash-Builtins,

根据http://www.gnu.org/software/bash/manual/bashref.html#Bash-Builtins

local [option] name[=value] ...

For each argument, a local variable named name is created, and assigned value. The option can be any of the options accepted by declare.

local [option] name[=value] ...

对于每个参数,都会创建一个名为 name 的局部变量,并为其分配值。该选项可以是declare 接受的任何选项。

So local -iis valid.

所以local -i是有效的。

回答by Paused until further notice.

declareinside a function automatically makes the variable local. So this works:

declare在函数内部自动使变量成为局部变量。所以这有效:

func() {
    declare -i number=0

    number=20
    echo "In ${FUNCNAME[0]}, $number has the value $number"
}

number=10
echo "Before the function, $number has the value $number"
func
echo "After the function, $number has the value $number"

And the output is:

输出是:

Before the function, $number has the value 10
In func, $number has the value 20
After the function, $number has the value 10

回答by not2qubit

In case you end up here with an Android shell script you may want to know that Android is using MKSH and not full Bash, which has some effects. Check this out:

如果您最终使用的是 Android shell 脚本,您可能想知道 Android 使用的是 MKSH 而不是完整的 Bash,后者会产生一些影响。看一下这个:

#!/system/bin/sh
echo "KSH_VERSION:  $KSH_VERSION"

local -i aa=1
typeset -i bb=1
declare -i cc=1

aa=aa+1;
bb=bb+1;
cc=cc+1;

echo "No fun:"
echo "  local   aa=$aa"
echo "  typset  bb=$bb"
echo "  declare cc=$cc"

myfun() {
    local -i aaf=1
    typeset -i bbf=1
    declare -i ccf=1

    aaf=aaf+1;
    bbf=bbf+1;
    ccf=ccf+1;

    echo "With fun:"  
    echo "  local   aaf=$aaf"
    echo "  typset  bbf=$bbf"
    echo "  declare ccf=$ccf"
}
myfun;

Running this, we get:

运行这个,我们得到:

# woot.sh
KSH_VERSION:  @(#)MIRBSD KSH R50 2015/04/19
/system/xbin/woot.sh[6]: declare: not found
No fun:
  local   aa=2
  typset  bb=2
  declare cc=cc+1
/system/xbin/woot.sh[31]: declare: not found
With fun:
  local   aaf=2
  typset  bbf=2
  declare ccf=ccf+1

Thus in Androiddeclaredoesn't exist. But reading up, the others should be equivalent.

因此在Androiddeclare中不存在。但是读起来,其他的应该是等价的。