I was wondering if the following two ways of running a bash script are equivalent?
. ./myScript.sh source myScript.sh
- Are they both running the content of the script instead of running the script, i.e. not creating a subshell for running the script?
They are equivalent in bash in that they do exactly the same thing. On the other hand,
sourceis 5 characters longer and is not portable to POSIX-only shells or Bourne whereas
.(dot) is, so I never bother using
That is correct - sourcing a file runs the commands in the current shell and it will affect your current shell environment. You can still pass arguments to the sourced file and bash will actually look in
$PATHfor the file name just like a normal command if it doesn't contain any slashes.
Not related to the original question of
source, but in your example,
is not identical to
source are functionally identical,
./myScript.sh are not the same. Since
./myScript.sh contains a slash, it's interpreted as a path and the shell just uses
myScript.sh does not have a slash so the shell does a
$PATH search for it first. This is the POSIX specified standard behavior for
.. Most shells default to this although they may add extensions (such as searching in the current working directory after the path search) or options to change the behavior of
Yes, they are equivalent. There is no functional difference;
. is just a builtin synonym for
(Edit: Apparently this is only true for
zsh. Some lighter shells don't have
. is specified by POSIX so
ash, etc. may not have
source. See jw013's answer for info.)