I know that source
and .
do the same thing, and I would be surprised to learn if the other pairs of commands in the title don't so the same thing (because I'm running bash as my shell, $SHELL [script]
and bash [script]
are equivalent, right??).
我知道这个来源和。做同样的事情,我会惊讶地发现标题中的其他命令对不是那么相同(因为我正在运行bash作为我的shell,$ SHELL [script]和bash [script]是相当于,对吗??)。
So what's the difference between the three methods of executing the script? I'm asking because I just learned that sourcing a script is NOT the exact same as executing it. In a way that I didn't find obvious from running my "experiments" and reading the man pages.
那么执行脚本的三种方法之间的区别是什么?我问,因为我刚刚得知采购脚本与执行脚本完全相同。在某种程度上,我没有从运行我的“实验”和阅读手册页中找到明显的方法。
What are the other subtle differences that I couldn't find by blindly calling these functions on incredibly simple scripts that I've written? After reading the above-linked answer, I can strongly guess that the answer to my question will be quite a simple explanation, but in a way that I'd almost never fully discover by myself.
通过在我写的非常简单的脚本上盲目地调用这些函数,我找不到其他微妙的差异?在阅读了上述相关答案之后,我可以强烈猜测,我的问题的答案将是一个非常简单的解释,但在某种程度上,我几乎从未完全发现自己。
Here's the "experiment" I did:
这是我做过的“实验”:
$. myScript.sh
"This is the output to my script. I'd like to think it's original."
$source myScript.sh
"This is the output to my script. I'd like to think it's original."
$bash myScript.sh
"This is the output to my script. I'd like to think it's original."
$$SHELL myScript.sh
"This is the output to my script. I'd like to think it's original."
$./myScript.sh
"This is the output to my script. I'd like to think it's original."
$myScript.sh
"This is the output to my script. I'd like to think it's original."
1 个解决方案
#1
11
. script
and source script
execute the contents of script
in the current environment, i.e. without creating a subshell. On the upside this allows script
to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script
to affect the current environment, which is a potential security hazard.
。脚本和源脚本在当前环境中执行脚本的内容,即不创建子shell。从好的方面来说,这允许脚本影响当前环境,例如更改环境变量或更改当前工作目录。在缺点方面,这允许脚本影响当前环境,这是潜在的安全隐患。
bash script
passes script
to the bash
interpreter to execute. Whatever shebang is given by script
itself is ignored. ("Shebang" referring to the first line of script
, which could e.g. read #!/bin/bash
, or #!/usr/bin/perl
, or #!/usr/bin/awk
, to specify the interpreter to be used.)
bash脚本将脚本传递给bash解释器来执行。无论什么shebang由脚本本身给予被忽略。 (“Shebang”指的是第一行脚本,可以读取#!/ bin / bash,或#!/ usr / bin / perl,或#!/ usr / bin / awk,指定要使用的解释器。)
$SHELL script
passes script
to whatever is your current shell interpreter to execute. That may, or may not, be bash
. (The environment variable SHELL
holds the name of your current shell interpreter. $SHELL
, if running bash, is evaluated to /bin/bash
, with the effect detailed in the previous paragraph.)
$ SHELL脚本将脚本传递给您要执行的当前shell解释器。这可能是,也可能不是。 (环境变量SHELL保存当前shell解释器的名称。$ SHELL,如果运行bash,则计算为/ bin / bash,其效果在前一段中详述。)
./script
executes the contents of a file script
in the current work directory. If there is no such file, an error is generated. The contents of $PATH
have no effect on what happens.
./script执行当前工作目录中的文件脚本的内容。如果没有此类文件,则会生成错误。 $ PATH的内容对发生的事情没有影响。
script
looks for a file script
in the directories listed in $PATH
, which may or may not include the current work directory. The first script
found in this list of directories is executed, which may or may not be the one in your current work directory.
脚本在$ PATH中列出的目录中查找文件脚本,该脚本可能包含也可能不包含当前工作目录。执行此目录列表中找到的第一个脚本,可能是也可能不是当前工作目录中的脚本。
#1
11
. script
and source script
execute the contents of script
in the current environment, i.e. without creating a subshell. On the upside this allows script
to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script
to affect the current environment, which is a potential security hazard.
。脚本和源脚本在当前环境中执行脚本的内容,即不创建子shell。从好的方面来说,这允许脚本影响当前环境,例如更改环境变量或更改当前工作目录。在缺点方面,这允许脚本影响当前环境,这是潜在的安全隐患。
bash script
passes script
to the bash
interpreter to execute. Whatever shebang is given by script
itself is ignored. ("Shebang" referring to the first line of script
, which could e.g. read #!/bin/bash
, or #!/usr/bin/perl
, or #!/usr/bin/awk
, to specify the interpreter to be used.)
bash脚本将脚本传递给bash解释器来执行。无论什么shebang由脚本本身给予被忽略。 (“Shebang”指的是第一行脚本,可以读取#!/ bin / bash,或#!/ usr / bin / perl,或#!/ usr / bin / awk,指定要使用的解释器。)
$SHELL script
passes script
to whatever is your current shell interpreter to execute. That may, or may not, be bash
. (The environment variable SHELL
holds the name of your current shell interpreter. $SHELL
, if running bash, is evaluated to /bin/bash
, with the effect detailed in the previous paragraph.)
$ SHELL脚本将脚本传递给您要执行的当前shell解释器。这可能是,也可能不是。 (环境变量SHELL保存当前shell解释器的名称。$ SHELL,如果运行bash,则计算为/ bin / bash,其效果在前一段中详述。)
./script
executes the contents of a file script
in the current work directory. If there is no such file, an error is generated. The contents of $PATH
have no effect on what happens.
./script执行当前工作目录中的文件脚本的内容。如果没有此类文件,则会生成错误。 $ PATH的内容对发生的事情没有影响。
script
looks for a file script
in the directories listed in $PATH
, which may or may not include the current work directory. The first script
found in this list of directories is executed, which may or may not be the one in your current work directory.
脚本在$ PATH中列出的目录中查找文件脚本,该脚本可能包含也可能不包含当前工作目录。执行此目录列表中找到的第一个脚本,可能是也可能不是当前工作目录中的脚本。