Can some one give an example where declare -x would be useful ?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
declare -x FOO
is the same as export FOO
. It "exports" the FOO
variable as an environment variable, so that programs you run from that shell session would see it.
回答2:
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:
# THIS IS NOT SAFE while test $# -gt 0; do eval export $1 shift done
with the safer:
while test $# -gt 0; do declare -x $1 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