171

What is the difference between executing a script (e.g. /some/script) with source /some/script and . /some/script in Bash?

apaderno
  • 289
binW
  • 13,034

2 Answers2

153

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.

apaderno
  • 289
  • where is this defined? I mean is . an alias for source or is this some thing else? – binW Feb 09 '11 at 18:26
  • exact same thing –  Feb 09 '11 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 see help source and help . shows the same info. – geirha Feb 09 '11 at 23:56
  • 3
    @binW: If you look in man bash and then search for source you'll see that source filename [arguments] is a synonym for . filename [arguments]. – Matthew Rankin Feb 17 '11 at 12:22
50

. 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.

jjg
  • 1,427
  • 17
    POSIX 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. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18 – geirha Feb 09 '11 at 23:51
  • You may be affected by the order of looking names up: http://unix.stackexchange.com/q/17815/8250 – Lekensteyn Jul 16 '12 at 11:05
  • 3
    This answer helped me, since sh is often used in cron. – dfrankow Mar 08 '13 at 14:25