What is the difference between executing a script (e.g. /some/script) with source /some/script
and . /some/script
in Bash?
2 Answers
source
and .
are synonymous in Bash.
For anyone who might like to verify that the commands are simply synonyms and nothing more, look at the source code, say for version 4.3, and examine the file builtins/source.def
. You will read that both of the built-in commands, source
and .
, use the very same function: source_builtin
.
-
where is this defined? I mean is . an alias for source or is this some thing else?– binWFeb 9, 2011 at 18:26
-
exact same thing– user8290Feb 9, 2011 at 18:42
-
14@binW:
.
is the traditional source command, inherited from the ancient bourne shell.source
is just a bash builtin that does exactly the same as.
, presumably because it's more readable (a.
alone may be hard to spot with a small font). You'll seehelp source
andhelp .
shows the same info.– geirhaFeb 9, 2011 at 23:56 -
3@binW: If you look in
man bash
and then search forsource
you'll see thatsource filename [arguments]
is a synonym for. filename [arguments]
. Feb 17, 2011 at 12:22
.
is synonymous with source
in bash, but not in POSIX sh, so you should use .
if your script is run by /bin/sh. Note that bash claims to run like POSIX sh when called as /bin/sh, but accepts source
without complaint.
This behaviour has bitten me, scripts tested with bash as /bin/sh fail when run under ash, for example.
-
17POSIX does not restrict the shell. POSIX just says; the shell shall support at least this and that feature. So a POSIX shell is free to implement additional features as long as the ones described by POSIX are implemented. When the shebang says
#!/bin/sh
you should never assume the shell supports anything but POSIX features though. pubs.opengroup.org/onlinepubs/9699919799/utilities/…– geirhaFeb 9, 2011 at 23:51 -
You may be affected by the order of looking names up: unix.stackexchange.com/q/17815/8250 Jul 16, 2012 at 11:05
-
3