Bash 中声明、排版和局部变量之间的区别
在 Bash 中输入变量时,declare
和 typeset
有什么区别?在函数内部使用时:declare
和 typeset
以及 local
之间有什么区别?
我遇到的唯一区别是排版可以移植到 ksh 脚本。除此之外,还有什么理由可以解释为什么其中一个应该优先于另一个呢?
更新:将 local
添加到问题中。
When typing variables in Bash, what is the difference between declare
and typeset
? When used inside a function: what is the difference between declare
and typeset
and local
?
The only difference I have come across is that typeset is portable to ksh scripts. Other than that, are there any reasons why one should be preferred over the other?
UPDATE: Added local
to the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
typeset
和declare
之间的区别:前者更可移植(例如 ksh),而当不考虑可移植性时,后者更可取。
declare
(或typeset
)和local
之间的区别:前者暗示后者,但更强大。例如,
declare -i x
使x
具有integer
属性,declare -r x
使x< /code> 只读等
typeset
anddeclare
:The former is more portable(e.g. ksh), while the latter is more preferable when portability is not a concern.
declare
(ortypeset
) andlocal
when used inside a function:The former implies the latter, but more powerful. For example,
declare -i x
makesx
have theinteger
attribute,declare -r x
makesx
readonly, etc.就bash而言,不,没有区别。事实上,联机帮助页让它们共享相同的定义
我还发现这个小花絮也进一步证实了我的主张正如您提到的 ksh 可移植性。
As far as bash is concerned, no, there is no difference. In fact, the manpage has them share the same definition
I also found this little tidbit which further substantiates my claim as well as the
ksh
portability you mentioned.在 Bash 手册的 4.2 Bash 内置命令 部分中,它指出:
In the Bash manual under section 4.2 Bash Builtin Commands it states:
“提供‘typeset’命令是为了与 Korn shell 兼容。它是‘declare’内置命令的同义词。” ---- 参考号。
信息“bash”“排版”
"The 'typeset' command is supplied for compatibility with the Korn shell. It is a synonym for the 'declare' builtin command." ---- Ref.
info "bash" "typeset"