声明和排版有什么区别

Rah*_*til 32 shell bash typeset

谁能用现实生活中的例子解释声明和排版之间的区别。

Chr*_*own 34

bash,typesetdeclare是完全一样的。唯一的区别是它typeset被认为是过时的。

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

    Obsolete.  See `help declare'.
Run Code Online (Sandbox Code Playgroud)

手册页甚至同时列出了它们:

declare [-aAfFgilrtux] [-p] [name[=value] ...]
typeset [-aAfFgilrtux] [-p] [name[=value] ...]
    Declare variables and/or give them attributes.
Run Code Online (Sandbox Code Playgroud)

typeset可移植到其他一些 shell,例如ksh93. 如果您的目标是跨外壳可移植性,请使用typeset(并确保您调用它的方式是可移植的)。如果您不关心这种便携性,请使用declare.

  • @ChrisDown,你不会碰巧知道为什么`typeset` 被认为已经过时了? (3认同)
  • 在 bash 4.4 及更高版本中,它已更改为**同义词**而不是已过时。`help typeset` => `\`declare' 的同义词。请参阅“帮助声明”。` (3认同)
  • 不错的。请注意,“过时”仅出现在“帮助排版”中,而不出现在“人排版”中。 (2认同)

Gil*_*not 6

declare我知道一个有助于避免邪恶的情况eval变量间接

$ var=foo
$ x=var
$ declare "$x=another_value"
$ echo $var
another_value
Run Code Online (Sandbox Code Playgroud)

  • 我的代码片段在评论中无法清除 (9认同)