Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-06-2008, 12:03 AM   #1
LQ Newbie
Registered: Aug 2007
Posts: 26

Rep: Reputation: 15
whats the difference between . (source) and ./

it looks the same to me that scripts being executed using . script (source script) or /path/script.
but are there any differences between these two methods of executing scripts?

Last edited by sean_zhang; 03-06-2008 at 12:04 AM.
Old 03-06-2008, 03:09 AM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
well source is like an include in c - take this file and insert it's contents here - , whereas ./ just defines execution of a file in the current directory - run this file. these things are conceptually extremely different but certainly could be used to a similar end.
Old 03-06-2008, 07:08 AM   #3
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Just one example of where a character can have different meanings depending on context.

The "." (source) is a command, whereas--in a different context-- ".", "./", "..", and various other forms are ways of designating directories.

"foo" means run the command "foo", which is found in of the directories listed in $PATH. "./foo" means run the "foo" found in the current directory (regardless of what is in $PATH.)
Old 03-06-2008, 07:34 AM   #4
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
The `.' builtin is equivalent to `source'.
Using either, the commands are executed in the same shell instead of launching the program in a new shell.

For security reasons, the current directory `.' isn't in the PATH variable. So to run a script in the current path, you need to explicitly indicate it's location, either relative to the current path `./' or by using the full pathname.
The command is run in it's own process. After that process exits your program continues.

If you run ./ and that program changes a variable, the change will not be retained when quits. If you run `source ./' the statements in will run in the same shell so they will be retained. Commonly in Linux, a configuration file will contain a number of 'name = value' statements. To set the variables in the config file, simply source it.
Here is an example line from /etc/profile:
    test -r $HOME/.bashrc && . $HOME/.bashrc
Old 03-06-2008, 10:27 AM   #5
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34

You can see the difference by following this example:
(I assume the shell used is bash, if you use <insert you favorite shell here>, you can surely transfer this...)

create a file with the following contents:

echo "The test variable is --$TESTVAR--"
make the file executable by you: chmod u+x

now execute the following in a terminal (bash# used to denote the prompt):
bash# TESTVAR="hello world"
bash# echo $TESTVAR
hello world
bash# .
The test variable is --hello world--
bash# ./
The test variable is ----
so what happened?
First, we set a variable TESTVAR in the current shell to "hello world". The variable is not passed to subcommands (you had to use export TESTVAR to do this).
On the next line, we printed the variable (the bash expanded its value and passed the value to the echo command, so technically, echo "hello world" is executed).
After that, we use the dot command to source our script file, that means the bash behaves as if the contents of the file was typed at that place where the dot command is written. So, technically a comment, an empty line and the third line (echo "The test variable is --$TESTVAR--") is executed. Of course, only the echo line is doing something and, as in the typed echo command before, the variable is expanded on the fly, so echo "The test variable is --hello world--" is executed.
In the next command, we execute the command, that means:
  1. open the file
  2. interpret the shebang in the first line to find the program that should execute the script (here: /bin/bash)
  3. call the given program with the remaining contents as input:
    1. create a new bash shell with default variables etc. (see man bash for more)
    2. execute echo "The test variable is --$TESTVAR--". The variable is again substituted, but this time, the variable TESTVAR is not set (remember the missing export command above!). So now, the executed line will look like this: echo "The test variable is ----"
  4. the program (here: the "second" bash) ends and the next prompt for our first shell is shown again.

Hope to make it clear, If you think this is useful, feel free to copy this to a tutorial, but give me the url.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats the difference between... jalldridge Linux - Newbie 5 05-23-2006 10:11 AM
whats the difference?? its_toby General 4 10-11-2005 10:35 PM
whats the difference between Source Mage and Sorcerer maximalred Linux - Distributions 1 10-23-2004 10:22 PM
Whats the difference... johnsonb125 Linux - Newbie 3 05-19-2004 11:09 PM
whats the difference? sixth_sense Linux - Security 1 04-29-2004 06:28 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:13 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration