且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

之间的区别是什么:". [脚本]"或“源[脚本]","bash [脚本]或$ SHELL [脚本]"和"./[脚本]".或"[脚本]"?

更新时间:2022-06-18 04:30:26

. scriptsource script在当前环境中执行script 的内容,即不创建子shell.从好的方面来说,这允许script影响当前环境,例如,更改环境变量或更改当前工作目录.不利的一面是,这会导致script影响当前环境,这是潜在的安全隐患.

. 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.

bash scriptscript传递给bash解释器以执行. script本身给出的任何shebang都将被忽略. ("Shebang"指的是script的第一行,例如可以读取#!/bin/bash#!/usr/bin/perl#!/usr/bin/awk,以指定要使用的解释器.)

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.)

$SHELL script会将script传递给,无论您当前使用的shell解释器是什么.都可以执行. bash可能是也可能不是. (环境变量SHELL保留当前shell解释器的名称.$SHELL如果运行bash,则评估为/bin/bash,其效果在上一段中进行了详细说明.)

$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.)

./script在当前工作目录中执行文件script 的内容.如果没有这样的文件,则会生成错误. $PATH的内容对发生的情况没有影响.

./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 ,该文件可能包含也可能不包含当前工作目录.在此目录列表中找到的第一个script被执行,它可能是当前工作目录中的一个,也可能不是.

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.