What is the difference between: ". [Script]" or "source [script]", "bash [script] or $ SHELL [script]" and "./[script]" or "[script]"?

I know what source and . doing the same thing, and I would be surprised to know that the other pairs of commands in the header are not the same (because I run bash because my shell $SHELL [script] and bash [script] equivalent, right?).

So what is the difference between the three ways to execute a script? I ask because I just found out that the source of the script is NOT the same as its execution. In a sense, what I did not find obvious due to the launch of my "experiments" and reading of man pages.

What are the other subtle differences that I could not find blindly calling these functions on the incredibly simple scripts I wrote? . After reading the answer above, I can guess that the answer to my question will be a fairly simple explanation, but in a way that I would almost never have discovered.

Here's the "experiment" I did:

 $. myScript.sh "This is the output to my script. I'd like to think it original." $source myScript.sh "This is the output to my script. I'd like to think it original." $bash myScript.sh "This is the output to my script. I'd like to think it original." $$SHELL myScript.sh "This is the output to my script. I'd like to think it original." $./myScript.sh "This is the output to my script. I'd like to think it original." $myScript.sh "This is the output to my script. I'd like to think it original." 
+5
source share
1 answer

. script . script and source script execute the contents of the script in the current environment, i.e. without creating a subshell. At the top, this allows the script affect the current environment, for example, change environment variables or change the current working directory. On the other hand, this allows the script affect the current environment, which is a potential security risk.

bash script passes the script the bash interpreter for execution. No matter what shebang is given by the script itself, it is ignored. ("Shebang"), referring to the first line of the script , which can, for example, read #!/bin/bash or #!/usr/bin/perl or #!/usr/bin/awk to indicate the interpreter to be used .)

$SHELL script passes the script all you need to execute the current shell interpreter. It may or may not be bash . (The SHELL environment variable contains the name of your current shell interpreter. $SHELL , if bash is executed, evaluates to /bin/bash , with the effect described in detail in the previous paragraph.)

./script executes the contents of the script file in the current working directory. If there is no such file, an error is generated. The contents of $PATH do not affect what happens.

script looks for the script file in directories listed in $PATH , which may or may not include the current working directory. The first script found in this directory listing is executed, which may or may not be specified in your current working directory.

+10
source

Source: https://habr.com/ru/post/1271007/


All Articles