Scoping problems in different shell languages?

回眸只為那壹抹淺笑 提交于 2019-12-02 11:19:14

问题


It appears that pdksh and mksh has the scoping implementation I expected.

For example:

readonly x='global'

f() {
  local x
  readonly x='f'
  echo $x
}

g() {
  local x
  readonly x='g'
  echo $x
}

echo $x

f 
g

echo $x

pdksh and mksh produce my expected result:

global
f
g
global

And Bash fails:

line 5: local: x: readonly variable

Dash and Ksh93 failed my expect, too. (I've changed local to typeset in Ksh93's test.)

This seems confusing.

UPDATE: I've edited the question. The question before is not stated in a clear way.


回答1:


Bash and Dash don't fail if the global variable is not read only.

Korn (ksh93) doesn't fail only if none of the instances of x are read only.



来源:https://stackoverflow.com/questions/5005903/scoping-problems-in-different-shell-languages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!