Bash 中声明、排版和局部变量之间的区别

发布于 2024-10-07 01:28:40 字数 259 浏览 5 评论 0原文

在 Bash 中输入变量时,declaretypeset 有什么区别?在函数内部使用时:declaretypeset 以及 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

め七分饶幸 2024-10-14 01:28:40
  • typesetdeclare 之间的区别:

前者更可移植(例如 ksh),而当不考虑可移植性时,后者更可取。

  • 在函数内部使用时,declare(或typeset)和local之间的区别:

前者暗示后者,但更强大。例如,declare -i x 使 x 具有 integer 属性,declare -r x 使 x< /code> 只读等

  • Difference between typeset and declare:

The former is more portable(e.g. ksh), while the latter is more preferable when portability is not a concern.

  • Difference between declare(or typeset) and local when used inside a function:

The former implies the latter, but more powerful. For example, declare -i x makes x have the integer attribute, declare -r x makes x readonly, etc.

无人问我粥可暖 2024-10-14 01:28:40

就bash而言,不,没有区别。事实上,联机帮助页让它们共享相同的定义

声明 [-aAfFilrtux] [-p]
[名称[=值] ...]
排版
[-aAfFilrtux] [-p] [名称[=值]
...]
声明变量和/或
赋予他们属性。如果没有名字
给出然后显示的值
变量。将显示 -p 选项
每个属性和值
名字...

我还发现这个小花絮也进一步证实了我的主张正如您提到的 ksh 可移植性。

声明或排版内置函数,
它们是精确的同义词,允许修改变量的属性。
这是一种非常弱的打字形式
[1] 在某些编程中可用
语言。声明命令是
特定于版本 2 或更高版本
猛击。排版命令也有效
在 ksh 脚本中。

As far as bash is concerned, no, there is no difference. In fact, the manpage has them share the same definition

declare [-aAfFilrtux] [-p]
[name[=value] ...]
typeset
[-aAfFilrtux] [-p] [name[=value]
...]
Declare variables and/or
give them attributes. If no names are
given then display the values of
variables. The -p option will display
the attributes and values of each
name...

I also found this little tidbit which further substantiates my claim as well as the ksh portability you mentioned.

The declare or typeset builtins,
which are exact synonyms, permit modifying the properties of variables.
This is a very weak form of the typing
[1] available in certain programming
languages. The declare command is
specific to version 2 or later of
Bash. The typeset command also works
in ksh scripts.

骑趴 2024-10-14 01:28:40

在 Bash 手册的 4.2 Bash 内置命令 部分中,它指出:

'排版'
排版 [-afFrxi] [-p] [名称[=值] ...]
提供“typeset”命令是为了与 Korn 兼容
壳;然而,它已被弃用,取而代之的是“声明”
内置命令。

In the Bash manual under section 4.2 Bash Builtin Commands it states:

'typeset'
typeset [-afFrxi] [-p] [NAME[=VALUE] ...]
The 'typeset' command is supplied for compatibility with the Korn
shell; however, it has been deprecated in favor of the 'declare'
builtin command.

拥有 2024-10-14 01:28:40

“提供‘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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文