What is the difference between '$ . foo' and '$ ./foo'?
t is a simple script
What is different between two case below :
$ . t
. means the current directory, so if
tis a simple script in
.t will give as a result
/home/someusert, and generating an error because it won’t exist.
/ will give the correct path to the script, so
./t will give as result
/home/someuser/t, that is the correct path to the script
. t won’t open a new shell to run t, so all variables created or modified will remain after execution. t will be searched for in $PATH, so if you wanted to run t from the path where you are right now the command should have been
./t will execute t which is in the path where we are right now spawning a shell for execution.
To clarify a little bit more. Supose t contains:
After you execute
./t you can
echo $data from the command line and you will get nothing, but if you run
. ./t and run
echo $data you will receive a hello on your screen.
$ . somescript.sh in this case the
. is a synonym of the
source built-in. It reads in the argument (
somescript.sh in my example,
t in yours) as though you had typed those commands in your current shell.
$ ./somescript.sh is executing the script
somescript.sh in its own shell. In this case the
. is used as the relative portion of the path to an executable. To execute a file in Bash on Linux and Unix (and Windows if you installed Bash), you need to provide the full path to the executable. Using
./ as the beginning of the path tells Bash to use your current directory instead.
In the second case, this is how you execute any executable from within Bash. In the first case, the file must be a Bash file.
The other notable effect from running a script in the same shell is that
exit command would exit your own shell. It’s common for scripts to check for env variables or file existence, e.g.:
if [ -z $JAVA_HOME ]; then
echo "JAVA_HOME variable is not set. Exit..."
source this script you won’t be able to see the error (in case you don’t have JAVA_HOME set). Your terminal will close or ssh session end or you’ll fall back to previuos user if you did
So it’s good to run scripts in a subshell by default, rather than sourcing them.