Difference between. / executable and. 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>
source to share
./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.
source to share