What is the difference between '$ . foo' and '$ ./foo'?

t is a simple script
What is different between two case below :

$ .  t


$ ./t

The dot . means the current directory, so if tis a simple script in /home/someuser then .t will give as a result /home/someusert, and generating an error because it won’t exist.

The slash / 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

Answered By: jcbermu

. 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

./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.

Answered By: YoMismo

$ . 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.

Answered By: Tritium21

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..."
        exit 1

If you 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 su, etc.

So it’s good to run scripts in a subshell by default, rather than sourcing them.

Answered By: reimai
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.