Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between: ". [script]" or "source [script]", "bash [script] or $SHELL [script]", and "./ [script]" or "[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.

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."
like image 808
Ari Sweedler Avatar asked Aug 18 '17 16:08

Ari Sweedler


People also ask

What is difference between source and bash?

There is a post Using source vs bash commands, which attempted to explain, but I could not clarify if the responder tried to say "source" ignores past definition and sets environment variables in current shell or bash ignores past definition and sets environment variables in current shell.

What is the difference between executing a bash script vs sourcing it?

Sourcing a script will run the commands in the current shell process. Changes to the environment take effect in the current shell. Executing a script will run the commands in a new shell process.

What is difference between shell script and bash script?

Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash. In practice, however, "shell script" and "bash script" are often used interchangeably, unless the shell in question is not Bash.

What is the difference between the DOT and source command?

source is a synonym for dot/period '. ' in bash, but not in POSIX sh, so for maximum compatibility use the period. When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes.


1 Answers

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

like image 166
DevSolar Avatar answered Sep 23 '22 04:09

DevSolar