之间的区别是什么:". [脚本]"或“源[脚本]","bash [脚本]或$ SHELL [脚本]"和"./[脚本]".或"[脚本]"? [英] What's the difference between: ". [script]" or "source [script]", "bash [script] or $SHELL [script]", and "./ [script]" or "[script]"?

查看:86
本文介绍了之间的区别是什么:". [脚本]"或“源[脚本]","bash [脚本]或$ SHELL [脚本]"和"./[脚本]".或"[脚本]"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道source.做相同的事情,并且我很惊讶地发现标题中的其他命令对不是一样(因为我像我一样运行bash) shell,$SHELL [script]bash [script]是等效的,对吧?).

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

那么执行脚本的三种方法之间有什么区别?我之所以问是因为,我刚刚了解到采购脚本是与执行时完全不同.从某种意义上说,我无法通过运行实验"和阅读手册页来了解这一点.

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.

这是我做的实验":

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

推荐答案

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

这篇关于之间的区别是什么:". [脚本]"或“源[脚本]","bash [脚本]或$ SHELL [脚本]"和"./[脚本]".或"[脚本]"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆