linux - What's the difference between: ". [script]" or "source [script]", "bash [script] or $SHELL [script]", and "./ [script]" or "[script]"? -


i know source , . same thing, , surprised learn if other pairs of commands in title don't same thing (because i'm running bash shell, $shell [script] , bash [script] equivalent, right??).

so what's difference between 3 methods of executing script? i'm asking because learned sourcing script not exact same executing it. in way didn't find obvious running "experiments" , reading man pages.

what other subtle differences couldn't find blindly calling these functions on incredibly simple scripts i've written? after reading above-linked answer, can guess answer question quite simple explanation, in way i'd never discover myself.

here's "experiment" did:

$. myscript.sh   "this output script. i'd think it's original."  $source myscript.sh   "this output script. i'd think it's original."  $bash myscript.sh   "this output script. i'd think it's original."  $$shell myscript.sh   "this output script. i'd think it's original."  $./myscript.sh    "this output script. i'd think it's original."  $myscript.sh    "this output script. i'd think it's original." 

. script , source script execute contents of script in current environment, i.e. without creating subshell. on upside allows script affect current environment, example changing environment variables or changing current work directory. on downside allows script affect current environment, potential security hazard.

bash script passes script bash interpreter execute. whatever shebang given script ignored. ("shebang" referring first line of script, e.g. read #!/bin/bash, or #!/usr/bin/perl, or #!/usr/bin/awk, specify interpreter used.)

$shell script passes script whatever current shell interpreter execute. may, or may not, bash. (the environment variable shell holds name of current shell interpreter. $shell, if running bash, evaluated /bin/bash, effect detailed in previous paragraph.)

./script executes contents of file script in current work directory. if there no such file, error generated. contents of $path have no effect on happens.

script looks file script in directories listed in $path, may or may not include current work directory. first script found in list of directories executed, may or may not 1 in current work directory.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -