t
is a simple script
What is different between two case below :
$ . t
and
$ ./t
t
is a simple script
What is different between two case below :
$ . t
and
$ ./t
. 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.
$ . 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..."
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.
The dot .
means the current directory, so if t
is 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
.
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
.
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
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 forsource
builtin that tells bash to take the contents oft
and behave as if you typed them into the current terminal. – orion Feb 19 '15 at 10:17bash
it may be a short notation, in other shells it is not. For instance,ksh
does not have thesource
command but does have the.
– YoMismo Feb 19 '15 at 10:31bash
. – orion Feb 19 '15 at 10:32