声明和排版之间有什么区别


Answers:


27

bashtypeset并且declare是完全一样的。唯一的区别是typeset被认为已过时。

typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...
    Set variable values and attributes.

    Obsolete.  See `help declare'.

手册页甚至还列出了它们:

declare [-aAfFgilrtux] [-p] [name[=value] ...]
typeset [-aAfFgilrtux] [-p] [name[=value] ...]
    Declare variables and/or give them attributes.

typeset可移植到其他一些外壳,例如ksh93。如果您希望跨外壳可移植性,请使用typeset(并确保您称呼它的方式可移植)。如果您不关心这种可移植性,请使用declare


您能否提供一些在bash脚本中在何处以及为什么使用它的示例
Rahul Patil

1
就像我说的一样,它们是完全一样的。我不明白如何区分它们。
克里斯·

2
好一个 请注意,“过时”仅出现在中help typeset,而不是中man typeset
fedorqui 2014年

2
@ChrisDown,您不会碰巧知道为什么typeset被认为已经过时了?
Alexej Magura

3

我知道在什么情况下declare可以避免邪恶eval变量间接

$ var=foo
$ x=var
$ declare "$x=another_value"
$ echo $var
another_value

这是一个很好的评论,但是它如何回答关于typeset和的问题declare
jw013

克里斯·
唐纳

也许我不清楚。评论应该是评论,答案应该是答案。这是评论,不是答案。
jw013 2013年

3
我的
摘要

但是您仍然可以使用typeset来分配变量...只要将声明替换为typeset,您仍然会得到相同的结果。
pcarvalho
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.