在一个shell中,有什么区别?
. executable
和
./executable
在第一个,点source
权利的捷径? 那么./executable
和source executable
有什么区别吗?
./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>
./executable
运行当前工作目录中的可执行文件。 ( executable
是不够的,如果$PATH
没有,通常没有)。 在这种情况下, executable
可以是一个elf二进制文件,或者是一个以#!/some/interpreter
开头的脚本,或者任何你可以exec
的脚本(在Linux上它可能是所有的东西 ,这要归功于binfmt
模块)。
. executable
. executable
将shell脚本导入当前shell,无论它是否具有执行权限。 没有新的进程被创建。 在bash
,根据$PATH
变量搜索脚本。 脚本可以设置环境变量,这些变量将保持在你的 shell中,定义函数和别名等等。
在第二个中给出路径: ./
是当前的工作目录,所以它不在PATH
搜索可执行文件,而是在当前目录中搜索。
source
将可执行文件作为参数并在当前进程中执行。