unix 命令行使用 .(点)与没有 [英] unix command line execute with . (dot) vs. without

查看:60
本文介绍了unix 命令行使用 .(点)与没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 unix 命令行中,通过简单地输入程序名称来执行程序与通过输入 .(点)后跟程序名称?例如:

runme

对比

<预><代码>.跑我

解决方案

.name 将名为 name 的文件导入当前 shell.所以如果一个文件包含这个

A=你好

然后,如果您获取它,之后您可以引用名为 A 的变量,该变量将包含 hello.但是如果你执行文件(给定适当的执行权限和 #!/interpreter 行),那么这样的事情将不起作用,因为脚本设置的变量和其他东西只会影响它的 子shell 运行.

获取二进制文件没有任何意义:Shell 不知道如何解释二进制文件(请记住,它会将出现在该文件中的内容插入到当前 shell 中 - 就像旧的 #include <file> 机制在 C 中).示例:

head -c 10/dev/urandom >foo.sh;.foo.sh # 不要在家里这样做!bash:  ǻD$/: 找不到文件或目录

当然,执行二进制文件确实很有意义.所以通常你只想命名你想要执行的文件,在特殊情况下,比如上面的 A=hello 情况,你想获取一个文件.

At a unix command line, what's the difference between executing a program by simply typing it's name, vs. executing a program by typing a . (dot) followed by the program name? e.g.:

runme

vs.

. runme

解决方案

. name sources the file called name into the current shell. So if a file contains this

A=hello

Then if you sources that, afterwards you can refer to a variable called A which will contain hello. But if you execute the file (given proper execution rights and #!/interpreterline), then such things won't work, since the variable and other things that script sets will only affects its subshell it is run in.

Sourcing a binary file will not make any sense: Shell wouldn't know how to interpret the binary stuff (remember it inserts the things appearing in that file into the current shell - much like the good old #include <file> mechanism in C). Example:

head -c 10 /dev/urandom > foo.sh; . foo.sh # don't do this at home!
bash: �ǻD$�/�: file or directory not found

Executing a binary file, however, does make a lot of sense, of course. So normally you want to just name the file you want to execute, and in special cases, like the A=hello case above, you want to source a file.

这篇关于unix 命令行使用 .(点)与没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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