Reputation: 16143
There are two options to run a shell script:
$ ./some/script.sh
$ . ./some/script.sh
As far as I unterstand, the first one starts a new shell environment based on the given shebang line withing the script. While the second one executes the statements within the same shell environemnt.
Are there more differences ?
Where can I find more documentation about the second one ?
Is .
a real command ? I can not find a manpage to it.
Upvotes: 1
Views: 118
Reputation: 182664
Are there more differences ?
The gist of the matter is that using .
the script is executed line by line in the same process. Otherwise a new process is forked. And a separate process has no way of changing the parent, for example it can't change environment variables such as the current directory.
Where can I find more documentation about the second one ?
[cnicutar@fresh ~]$ help source
source: source filename [arguments]
...
Is . a real command
[cnicutar@fresh ~]$ type .
. is a shell builtin
In case it isn't obvious already, .
and source
are identical*.
As rush commented, source
isn't specified by POSIX so you should probably use .
in code intended to be portable. The dot is specified in chapter 2.
Upvotes: 4
Reputation: 59627
.
is a Bourne Shell command for reading a file and executing the commands in the file, your analysis is essentially correct. bash and other shells add source
as an alias for .
.
See the manual for bash builtins, and see .
at the top of the manual for Bourne sh builtins.
Read and execute commands from the filename argument in the current shell context. If filename does not contain a slash, the PATH variable is used to find filename. When Bash is not in posix mode, the current directory is searched if filename is not found in $PATH. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the exit status of the last command executed, or zero if no commands are executed. If filename is not found, or cannot be read, the return status is non-zero. This builtin is equivalent to source.
Upvotes: 1
Reputation: 1088
it is used to source an environment. such as the .profile.
Upvotes: 0