In UNIX, when you want to run a shell script located in pwd, you do:
./somescript.sh
But there is also:
. somescript.sh
What does the second command do?
In UNIX, when you want to run a shell script located in pwd, you do:
./somescript.sh
But there is also:
. somescript.sh
What does the second command do?
The dot is an alias for the command "source": http://ss64.com/bash/source.html.
The main difference is that the first syntax tries to execute the script by running some interpreter for it (as determined by the hashbang magic header value). For a shell file, the interpreter is usually bash or sh, and so your shell will launch a new shell process as a subprocess and pass the script as a parameter. The script will run isolated in this subprocess. If it for instance sets an environment variable, it will beisolated to the subprocess and disappear as the subprocess exits
Sourcing the file, OTOH, instructs the current shell to read the instructions in said file. In this case changes will modify the current environment. Changed environment variables will be visible after command completion.
Sourcing only works for shell scripts written for the current shell. Execution works for any type of runnable script/program/executable file.