164

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

2 Answers 2

146

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.

4
  • where is this defined? I mean is . an alias for source or is this some thing else?
    – binW
    Feb 9, 2011 at 18:26
  • exact same thing
    – user8290
    Feb 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 see help source and help . shows the same info.
    – geirha
    Feb 9, 2011 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]. Feb 17, 2011 at 12:22
48

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

3
  • 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. pubs.opengroup.org/onlinepubs/9699919799/utilities/…
    – geirha
    Feb 9, 2011 at 23:51
  • You may be affected by the order of looking names up: unix.stackexchange.com/q/17815/8250
    – Lekensteyn
    Jul 16, 2012 at 11:05
  • 3
    This answer helped me, since sh is often used in cron.
    – dfrankow
    Mar 8, 2013 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.