函数本地只读与全局只读变量具有相同名称

use*_*735 14 bash scope

当我有一个函数本地只读变量和具有相同名称的全局只读变量时,我会感到惊讶.

从全局声明中删除只读选项时.即

declare -r var="main"
Run Code Online (Sandbox Code Playgroud)

改为:

declare var="main"
Run Code Online (Sandbox Code Playgroud)

我得到了预期的行为.我一直在阅读bash手册页,但我找不到这种行为的解释.能否请您指出手册中解释问题的部分?

我认为这是一个类似的问题,而不是如何在不同的shell语言中支持词法作用域?但更具体.

细节:

$ cat readonly_variable.sh 
#!/bin/bash

# expected output:
#
# BASH_VERSION = 3.2.25(1)-release
# function
# main
#
# but instead getting:
#
# BASH_VERSION = 3.2.25(1)-release
# ./readonly_variable.sh: line 6: local: var: readonly variable
# main
# main
#
# when read-only option (-r) is removed from global declaration (*), the output
# is expected

set -o nounset

function func {
  local -r var="function"
  echo "$var"
}

declare -r var="main" # (*)

echo BASH_VERSION = $BASH_VERSION
echo $(func)
echo $var

exit 0
Run Code Online (Sandbox Code Playgroud)

我坚持这个特别的Bash版本.

$ ./readonly_variable.sh
BASH_VERSION = 3.2.25(1)-release
./readonly_variable.sh: line 24: local: var: readonly variable
main
main
$
Run Code Online (Sandbox Code Playgroud)

zar*_*dav 12

实际上,出于安全原因,明确禁止制作只读全局变量的本地副本,如bash源代码(in variables.c:make_local_variable)中所述:

针对old_var的上下文级别的测试是禁止只读全局变量的本地副本(因为"我"认为这可能是一个安全漏洞).

("我"不是我,我只是引用)

/* Since this is called only from the local/declare/typeset code, we can
   call builtin_error here without worry (of course, it will also work
   for anything that sets this_command_name).  Variables with the `noassign'
   attribute may not be made local.  The test against old_var's context
   level is to disallow local copies of readonly global variables (since I
   believe that this could be a security hole).  Readonly copies of calling
   function local variables are OK. */
if (old_var && (noassign_p (old_var) ||
   (readonly_p (old_var) && old_var->context == 0)))
{
  if (readonly_p (old_var))
    sh_readonly (name);
  return ((SHELL_VAR *)NULL);
}
Run Code Online (Sandbox Code Playgroud)

  • 不是我要去看文件的第一个地方. (3认同)