Shell脚本中的奇怪变量定义 [英] strange variable definition in shell script

查看:49
本文介绍了Shell脚本中的奇怪变量定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

test.sh:

#! /bin/sh
me=I ./test2.sh

test2.sh:

#! /bin/sh
echo $me

运行脚本1并打印:

[zhibin@szrnd1 sh]$ ./test.sh
I
[zhibin@szrnd1 sh]$ 

如所见,变量"$ me"被传输到"test2.sh".

As see, the variable "$me" be transferred to "test2.sh".

我在谷歌搜索中没有发现变量定义的这种用法,有人可以告诉我在哪里可以找到包括上述用法的教程吗?

I didn't find this usage of variable definition on googling, can someone tell me where can find the tutorial including the usage above mentioned?

非常感谢!

推荐答案

由于已经在很多方面提到了这一点,所以我假设您正在寻找有关它的文档.除了 BASH文档关于此内容:

Since this has been mentioned on SO a lot, I'm assuming you're looking for some documentation on it. I'm not sure there is anything more detailed than the BASH documentation about this:

如Shell Parameters中所述,可以通过在其前面添加参数分配来临时扩展任何简单命令或功能的环境.这些赋值语句仅影响该命令看到的环境.

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

通过实验可以看到,当您执行"A = B命令"时,它将运行该命令,就像在该命令之前运行了"export A = B"一样,然后在命令完成后A的值恢复为之前的值.这是将某些环境传递到命令同时又确保脚本的其余部分不受影响的一种非常方便的方法.

As you've seen by experimenting, when you do "A=B command", it runs the command as if "export A=B" was run just prior to that command then A's value reverts to its previous after command completes. It's a very convenient way to pass some environment into a command while ensuring the rest of the script is not affected.

这篇关于Shell脚本中的奇怪变量定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆