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

前端之家收集整理的这篇文章主要介绍了bash – 具有相同名称的本地只读与全局只读变量的函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我有一个函数本地只读变量和具有相同名称的全局只读变量时,我会得到一个惊人的行为.

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

declare -r var="main"

改为:

declare var="main"

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

我认为这是一个类似的问题,比How does lexical scoping supported in different shell languages?更具体.

细节:

$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

我陷入了这个特殊的Bash版本.

$./readonly_variable.sh
BASH_VERSION = 3.2.25(1)-release
./readonly_variable.sh: line 24: local: var: readonly variable
main
main
$
实际上,为了安全起见,明确地禁止只读全局变量的本地副本,如bash源代码(在variables.c:make_local_variable中)所示:

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).

(其中“我”不是我,我只是引用)

/* 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);
}
原文链接:https://www.f2er.com/bash/385779.html

猜你在找的Bash相关文章