使用 ./script.sh 和 ../script.sh [英] Difference between launching a script with ./script.sh and . ./script.sh
问题描述
请告诉我 bash shell 与启动脚本有什么区别./script.sh
和 ../script.sh
?
Please tell me what is the difference in bash shell between launching a script with
./script.sh
and . ./script.sh
?
推荐答案
正如 klausbyskov 所说,第一种形式要求文件设置了可执行权限位.
As klausbyskov says, the first form requries that the file have its executable permission bit set.
但更重要的是,第一种形式在单独的进程中执行脚本(与启动它的 shell 不同、独立并且无法在其中进行更改).第二种形式使初始 shell 直接运行文件中的命令(就像您将它们输入到 shell 中,或者好像它们包含在执行采购"的脚本中一样).
But more importantly, the first form executes the script in a separate process (distinct from, independent of, and unable to make changes in the shell that launched it). The second form causes the initial shell to directly run the commands from the file (as if you had typed them into the shell, or as if they were included in the script that does the ‘sourcing’).
包含 FOO=bar 的脚本;export FOO
不会在运行第一个变体的 shell 中创建导出的 FOO
环境变量,但它会在运行第二个变体的 shell 中创建这样一个变量.
A script that contains FOO=bar; export FOO
will have not create an exported FOO
environment variable in the shell that runs the first variant, but it will create such a variable in a shell that runs the second variant.
第二种形式(‘sourcing’)有点像 C 中的 #include
.
The second form (‘sourcing’) is a bit like a #include
in C.
这篇关于使用 ./script.sh 和 ../script.sh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!