【发布时间】:2013-01-15 13:01:06
【问题描述】:
在shell中,有什么区别?
. executable
和
./executable
在第一个中,点是source 的快捷方式,对吗?那么./executable和source executable有区别吗?
【问题讨论】:
标签: linux bash shell executable
在shell中,有什么区别?
. executable
和
./executable
在第一个中,点是source 的快捷方式,对吗?那么./executable和source executable有区别吗?
【问题讨论】:
标签: linux bash shell executable
在第二个中,您提供路径:./ 是当前工作目录,因此它不会在 PATH 中搜索可执行文件,而是在当前目录中搜索。
source将可执行文件作为参数,在当前进程中执行。
【讨论】:
source 不启动子进程。
./executable 运行当前工作目录中的可执行文件。 (如果你的$PATH 中没有.,那么executable 是不够的,而且通常没有)。在这种情况下,executable 可以是一个精灵二进制文件,或者一个以#!/some/interpreter 开头的脚本,或者你可以使用exec 的任何东西(在Linux 上它可能是一切,感谢binfmt 模块) .
. executable 将 shell 脚本 输入到您当前的 shell 中,无论它是否具有 执行 权限。不会创建新进程。在bash 中,根据$PATH 变量搜索脚本。脚本可以设置环境变量,这些变量将在 你的 shell 中保持设置,定义函数和别名等等。
【讨论】:
./executable 和 source executable 之间有区别吗?
基本区别是,
./foo.sh - foo.sh will be executed in a sub-shell
source foo.sh - foo.sh will be executed in current shell
一些例子可以帮助解释差异:
假设我们有foo.sh:
#!/bin/bash
VAR=100
来源:
$ source foo.sh
$ echo $VAR
100
如果你:
./foo.sh
$ echo $VAR
[empty]
另一个例子,bar.sh
#!/bin/bash
echo "hello!"
exit 0
如果你像这样执行它:
$ ./bar.sh
hello
$
但如果你来源它:
$ source bar.sh
<your terminal exits, because it was executed with current shell>
【讨论】: