Difference between. / executable and. executable
In a shell, what's the difference between?
. executable
and
./executable
In the first, the hot spot is for source
right? So is there a difference between ./executable
and source executable
?
is there any difference between. / executable and the original executable?
The main difference,
./foo.sh - foo.sh will be executed in a sub-shell
source foo.sh - foo.sh will be executed in current shell
Some example might help explain the difference:
let's say we have foo.sh
:
#!/bin/bash
VAR=100
enter it:
$ source foo.sh
$ echo $VAR
100
If you:
./foo.sh
$ echo $VAR
[empty]
another example, bar.sh
#!/bin/bash
echo "hello!"
exit 0
if you execute it like:
$ ./bar.sh hello $
but if you use it:
$ source bar.sh
<your terminal exits, because it was executed with current shell>
./executable
launches an executable file located in the current working directory. ( executable
not enough for this if $PATH
not .
, and usually it isn't). In this case, it executable
could be a binary elf or a script starting with #!/some/interpreter
, or whatever you can exec
(on Linux, that's potentially everything, thanks to the module binfmt
).
. executable
prints a shell script into your current shell, whether it has execute permissions or not. No new process is created. The bash
script is searched according to the variable $PATH
. The script can set environment variables that will stay set in your shell, define functions and aliases, etc.
In the second, you specify the path: ./
- the current working directory, so it does not search in PATH
for the executable, but in the current directory.
source
takes an executable file as a parameter and executes it in the current process.