如何在 bash 中使用声明 -x
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5785668/
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
How to use declare -x in bash
提问by Ankur Agarwal
Can some one give an example where declare -x would be useful ?
有人可以举一个例子,其中 declare -x 会有用吗?
采纳答案by Chris Jester-Young
declare -x FOOis the same as export FOO. It "exports" the FOOvariable as an environment variable, so that programs you run from that shell session would see it.
declare -x FOO与 相同export FOO。它将FOO变量“导出”为环境变量,以便您从该 shell 会话运行的程序会看到它。
回答by William Pursell
Declare -x can be used instead of eval to allow variables to be set as arguments to the shell. For example, you can replace the extremely insecure:
可以使用声明 -x 代替 eval 来允许将变量设置为 shell 的参数。例如,您可以替换极其不安全的:
# THIS IS NOT SAFE while test $# -gt 0; do eval export shift done
with the safer:
更安全:
while test $# -gt 0; do declare -x shift done
As an aside, this construct allows the user to invoke the script as:
顺便说一句,这个构造允许用户调用脚本:
$ ./test-script foo=bar
rather than the more idiomatic (but confusing to some):
而不是更惯用的(但对某些人来说令人困惑):
$ foo=bar ./test-script

