LinuxQuestions.org
Review your favorite Linux distribution.
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 01-04-2008, 12:55 AM   #1
muazfarooqaslam
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Rep: Reputation: 15
Difference between executing & sourcing a script


Hi,

Can anybody please explain the difference between executing a script and sourcing a script and what is the purpose of both.

rgds
 
Old 01-04-2008, 02:51 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
If you execute a script by setting the executable permission and then calling it by it's full or relative path, or just by name if it is in the PATH, then a new shell process will be created to interpret the script (the shell type determined by the shebang line at the start of the script).

If you source a script either by using the source keyword or the . syntax, i.e. one of these:
Code:
source test.sh
. test.sh
...then the file will be read and executed in the current shell.

There are a few implications of using source over executing a script:
  • Environment variables and other shell variables which are set during the course of the script's run will still be available after the script's contents have completed running.
  • If you call exit from the sourced script, it will terminate the calling script / interactive shell.
  • You won't incur the overhead of starting a new shell process. For one-off calls this isn't so interesting, but if you want to execute a script many times in a loop it might make a useful difference.
 
  


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
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 05:05 PM
executing a script!! vishamr2000 Linux - Security 5 07-29-2005 02:34 AM
Executing install script for ... securehack Linux - Software 6 05-24-2005 11:02 AM
Difference Executing In GUI And Konsole Shing Linux - Newbie 2 04-05-2005 11:19 AM
Script is not executing in Apache!!! linux_child001 Linux - Newbie 1 06-22-2004 05:10 AM

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

All times are GMT -5. The time now is 08:39 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