15

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

$ .  t

and

$ ./t

4 Answers4

13

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

YoMismo
  • 4,015
  • 3
    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. – orion Feb 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 . – YoMismo Feb 19 '15 at 10:31
  • I know, I added this because the tag and the question title say bash. – orion Feb 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 Ongaro Feb 19 '15 at 20:13
5

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

Tritium21
  • 152
0

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
fi

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.

reimai
  • 61
-2

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

jcbermu
  • 4,736
  • 18
  • 26
  • 1
    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 Voigt Feb 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. – sleblanc Feb 23 '15 at 02:54