marked as duplicate by Gilles bashUsers with the bash badge can single-handedly close bash questions as duplicates and reopen them as needed.Feb 19 '15 at 23:02

4 Answers
4

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

#!/bin/bash
data=hello

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.

I'd just add that t does not have to be a script in the second case - it can be any executable (and it must have the executable permission). In the first case, . is just short notation for source builtin that tells bash to take the contents of t and behave as if you typed them into the current terminal.
– orionFeb 19 '15 at 10:17

In bash it may be a short notation, in other shells it is not. For instance, ksh does not have the source command but does have the .
– YoMismoFeb 19 '15 at 10:31

I know, I added this because the tag and the question title say bash.
– orionFeb 19 '15 at 10:32

1

You should mention that if a script is supposed to be sourced, that it's better to remove the shebang line and the executable flag. Otherwise people will execute the script and wondering why it's not "working".
– David OngaroFeb 19 '15 at 20:13

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

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.

Think about the claim that a . which is a substring of a filename gets expanded to the current working directory, and its implications for cd .. and cat info.txt
– Ben VoigtFeb 19 '15 at 17:51

This is utterly wrong. In this context, the . is a functional alias for source, so . t means "read the lines from the file t as if I typed them into my shell", like other answers say.
– sleblancFeb 23 '15 at 2:54