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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4419704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:56:41  来源:igfitidea点击:

Differences between declare, typeset and local variable in Bash

bashshellvariablesscopedeclare

提问by lecodesportif

When typing variables in Bash, what is the difference between declareand typeset? When used inside a function: what is the difference between declareand typesetand local?

在 Bash 中键入变量时,declare和之间有什么区别typeset?在函数内部使用时:declareandtypeset和之间有什么区别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?

我遇到的唯一区别是排版可以移植到 ksh 脚本。除此之外,还有什么理由让一个人比另一个人更受欢迎?

UPDATE: Added localto the question.

更新:添加local到问题中。

回答by Hui Zheng

  • Difference between typesetand declare:
  • typeset和之间的区别declare

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

前者更便于携带(例如 ksh),而后者在不考虑可移植性时更可取。

  • Difference between declare(or typeset) and localwhen used inside a function:
  • declare(or typeset) 与local在函数内部使用时的区别:

The former implies the latter, but more powerful. For example, declare -i xmakes xhave the integerattribute, declare -r xmakes xreadonly, etc.

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

回答by SiegeX

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

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

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

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

I also found this little tidbitwhich further substantiates my claim as well as the kshportability you mentioned.

我还发现了这个小花絮,它进一步证实了我的主张以及ksh您提到的便携性。

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.

声明或排版内置函数 是完全同义词,允许修改变量的属性。这是某些编程语言中可用的类型 [1] 的一种非常弱的形式。声明命令特定于版本 2 或更高版本的 Bash。typeset 命令也适用于 ksh 脚本。

回答by Paused until further notice.

In the Bash manual under section 4.2 Bash Builtin Commandsit states:

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

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

'
typeset ' typeset [-afFrxi] [-p] [NAME[=VALUE] ...]
提供 'typeset' 命令是为了与 Korn shell 兼容;然而,它已被弃用,取而代之的是 'declare' 内置命令。