Difference between ./executable and . executable
./executable
runs an executable which is in the current working directory. (executable
is not enough for that if there is no .
in your $PATH
, and usually there isn't). In this case, executable
can be an elf binary, or a script starting with #!/some/interpreter
, or anything you can exec
(on Linux it's potentially everything, thanks to binfmt
module).
. executable
sources a shell script into your current shell, whether it has execute permissions or not. No new process is created. In bash
, script is searched according to the $PATH
variable. Script may set environment variables which will remain set in your shell, define functions and aliases and so on.
is there a difference between ./executable and source executable?
basic difference is,
./foo.sh - foo.sh will be executed in a sub-shellsource foo.sh - foo.sh will be executed in current shell
some example could help to explain the difference:
let's say we have foo.sh
:
#!/bin/bashVAR=100
source it:
$ source foo.sh $ echo $VAR100
if you :
./foo.sh$ echo $VAR[empty]
another example, bar.sh
#!/bin/bashecho "hello!"exit 0
if you execute it like:
$ ./bar.shhello$
but if you source it:
$ source bar.sh<your terminal exits, because it was executed with current shell>
In the second one you give the path: ./
is the current working directory so it doesn't search in PATH
for the executable but in the current directory.
source
takes the executable as a parameter and executes it in the current process.