LinuxQuestions.org
Visit Jeremy's Blog.
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-12-2010, 05:09 AM   #1
abhayrathod
LQ Newbie
 
Registered: Apr 2009
Location: Maharashtra
Distribution: Fedora, FreeBSD
Posts: 5

Rep: Reputation: 0
Difference while executing script as . and ./


Hi

What is the difference when we execute a sh script like

Quote:
hostname#. script.sh
and

Quote:
hostname#./script.sh
Can any one please explain the difference?

Thanks in advance
 
Old 03-12-2010, 05:29 AM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 4&5, Fedora 10, CentOS 5.4, IPCop
Posts: 569

Rep: Reputation: 55
Let me address them in reverse order. The second syntax (./script.sh) tells your shell to look in the current directory for the script, and run it in a sub-shell. Any variables the script sets or changes are not available to the parent shell. This syntax is only needed if the current directory is not in the PATH. Otherwise you can just say script.sh and it will be found in the PATH and run. The ./ just means "look here, and nowhere else".

The second syntax (. script.sh - or even ". ./script.sh" if the script is in your current directory) is called "sourced". The script is run in the current shell, rather than a sub-shell and the variables it changes or creates are visible to the parent shell when the script terminates. I probably don't understand the full nuances of this approach, but I use it to set or change variables so they are available in the parent shell.

Last edited by blacky_5251; 03-12-2010 at 05:31 AM.
 
Old 03-12-2010, 05:59 AM   #3
abhayrathod
LQ Newbie
 
Registered: Apr 2009
Location: Maharashtra
Distribution: Fedora, FreeBSD
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for the answer. My concern was also about env variables not setting from script.

Thanks
 
Old 03-12-2010, 07:27 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I have seen "sourcing" most often as part of scripts. In a script, ". /path/somescript" means "make somescript part of the current script" (which, by definition, means run it in the same shell)

Quote:
My concern was also about env variables not setting from script.
OK, what's the question?
 
  


Reply

Tags
shell


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
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM
Run an external script, without holding up the currently executing script onesikgypo Programming 2 03-07-2009 09:26 PM
bash - difference between executing program in for loop and typing each command? jlarsen Programming 14 11-10-2008 05:38 PM
Difference between executing & sourcing a script muazfarooqaslam Linux - Newbie 1 01-04-2008 02:51 AM
Difference Executing In GUI And Konsole Shing Linux - Newbie 2 04-05-2005 11:19 AM


All times are GMT -5. The time now is 10:38 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration