The difference between sourcing a dot-script with
source (or with
., which is the standard command) and running it as its own script has been discussed before.
See e.g. "What is the difference between sourcing ('.' or 'source') and executing a file in bash?". It comes down to the difference between running a script in its own separate environment (without
.) and running a set of commands in the current shell environment, which is what you want to do if you need to set environment variables and shell options etc. for the current shell. Setting a shell option or exporting a variable in a script (not run with
.) will not affect the parent shell.
The particular test that you ask about,
[ -z "$PS1" ] && return
will return from the dot-script if the primary prompt,
$PS1, is empty. This is one way of testing whether the current shell session is interactive or not. If it's not interactive, the
PS1 variable will not be set and the prompt will be empty, in which case, the rest of the script (presumably) does not need to be run as it maybe only deals with things that needs initialising in an interactive shell.
A dot-script would need to use
return rather than
exit as an
exit would terminate the current shell session, which is the same as the session that invoked the script with
.. A dot-script thus behaves a whole lot like a shell function.