LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-06-2008, 12:03 AM   #1
sean_zhang
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?
thanks!

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

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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 ./program.sh and that program changes a variable, the change will not be retained when program.sh quits. If you run `source ./program.sh' the statements in program.sh 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:
Code:
    test -r $HOME/.bashrc && . $HOME/.bashrc
 
Old 03-06-2008, 10:27 AM   #5
doc.nice
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34
Lightbulb

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 test-inc.sh with the following contents:
Code:
#!/bin/bash

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

now execute the following in a terminal (bash# used to denote the prompt):
Code:
bash# TESTVAR="hello world"
bash# echo $TESTVAR
hello world
bash# . test-inc.sh
The test variable is --hello world--
bash# ./test-inc.sh
The test variable is ----
bash#
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 test-inc.sh, 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.
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:34 AM.

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