Possible Duplicate:
running script with “. ” and with “source ”

I have used both the dot command '.' and 'source' to reload a given rc file (typically to update my environment variables) but I am not sure if they are different and if one is preferred. What is the difference between the two ?

marked as duplicate by jw013, derobert, George M, Michael Mrozek Dec 14 '12 at 18:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


. is the Bourne and POSIX shell command while source is the C-Shell command.

Some Bourne-shell derivatives like bash, zsh and most implementations of ksh also have a source command which is generally an alias for . though for ksh with differences.

For bash and zsh, . and source behave the same, but their behavior is affected by whether they run in POSIX mode or not¹.

POSIX requires that the . command exits the shell if it can't open the file for reading and requires that the file be found through a search of the directories in $PATH if the provided path doesn't contain a /.

zsh and bash . and source behave as POSIX requires when in POSIX mode, and as pdksh's source when not, that is they don't exit the script if they fail to open the file for reading (same as command .) and lookup the file in $PATH and the current directory if the provided path doesn't contain a /.

AT&T ksh's source doesn't exit the shell either but doesn't look for the file in the current directory.

All in all, in Bourne-like shells (though not the Bourne shell that doesn't have a command builtin), if you want a consistent behavior, you could do

command . the-file-to-source || handle-error

And if the-file-to-source is meant to be in the current directory, be sure to write:

command . ./the-file-to-source || handle-error

In sh scripts (where sh is a POSIX sh) you should be able to rely on the POSIX behavior stated above.

¹ zsh and bash enable the POSIX mode when called as sh. For bash, also when it receives POSIXLY_CORRECT in its environment (even when called as bash even though there's no POSIX command called bash), or when it receives SHELLOPTS=posix, or when called with bash --posix or bash -o posix or after a set -o posix. With zsh, you use emulate sh to emulate sh. Emulations alter a whole bunch of options that change the behavior of zsh. In this case, the option is POSIX_BUILTINS.

In bash, you can check if in POSIX mode or not with the (non-POSIX), [ -o posix ] command. In zsh, you check the output of emulate to see if you're in sh emulation, or [[ -o posixbuiltins ]] to check whether that particular option is enabled. You can also temporarily enable a given emulation mode with emulate -L (to emulate in the current local scope only).

  • Great answer, do you also happen to know, if you use bash or zsh, how do you find out what mode you're in? I mean, save for testing with a file that does not exist. – Emanuel Berg Dec 17 '12 at 22:44

source is there for readability and self-documentation, . exists because it is quick to type. The commands are identical. Perl has long and short versions of many of its control variables for the same reason.

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