difference between source, exec and ./script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers difference between source, exec and ./script
# 1  
Old 03-23-2001
What is the difference between sourcing a script, running it or execing it?
# 2  
Old 04-15-2001
./script creates a new shell and executes each command in the script within the new shell. When the end of the script file is encountered, the new shell exits. Any changes in the new shell caused by the script are lost when the shell exits.

For example, if the file
/home/user/sample/script/test contains...

cd /usr/games
pwd
echo hi

Then the command sequence would yield the following output:

prompt>cd /home/user/sample/script
prompt>pwd
/home/user/sample/script
prompt>chmod +x test
prompt>./test
/usr/games
hi
prompt>pwd
/home/user/sample/script

source Execution
source execute a shell script within the context of the current shell. Since execution takes place within the context of the current shell, any changes in the shell are retained following the completion of the shell.
Example:

prompt>cd /home/user/sample/script
prompt>pwd
/home/user/sample/script
prompt>source test
/usr/games
hi
prompt>pwd
/usr/games


Execing a <command> (ie. shell script or executable) means give exec <command> on the shell prompt.

The exec command will execute a command in place of the current shell, that is, it terminates the current shell and starts a new process in its place.

exec was often used to execute the last command of a shell script. This would kill the shell slightly earlier; otherwise, the shell would wait until the last command was finished. This practice saves a process and some memory.

try exec ls. you will be logged out from your login shell.

exec also manipulates file descriptors in the Bourne shell.
$exec >>logoutput
after issuing this command you will not see output of any command in your console. all output goes into logoutput file.

use exec >/dev/console to return.

$exec 2>errs.out
means that from then on, stderr goes into errs.out file


hope this will help you

[Edited by mib on 04-15-2001 at 12:14 PM]
These 2 Users Gave Thanks to mib For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Difference: 'source file.sh' or './file.sh'

Dear all, I would like to know the difference in executing the shell script both ways? a.) source file.sh b.) ./file.sh I need to initialize some setup which is part of the file.sh. I realized that when I do the source file.sh the setup is all initlaized well. However, with the ./file.sh... (3 Replies)
Discussion started by: emily
3 Replies

2. Shell Programming and Scripting

Use of exec command in a script

Guru's, I want to make a use of "exec" command in my script and want to check return code of executing script, but as you know exec command will terminate current processID and comeout and will trigger new one, i am unable to check return code of script and not able to run a scrpit after exec. ... (2 Replies)
Discussion started by: pawar.atul28
2 Replies

3. Shell Programming and Scripting

Difference b/w xargs and "-exec" in Find

Hi, What is the difference between the following commands find . -type f -exec grep 'abc' {} \; and find . -type f | xargs grep 'abc' Appreciate your help. (2 Replies)
Discussion started by: bobbygsk
2 Replies

4. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

5. Programming

difference bewteen pipe, xargs, and exec

I have read several docs on these on the web and looked at examples. I can't figure out the difference. In some cases you use one or the other or you combine them. can someone help me understand this? (1 Reply)
Discussion started by: guessingo
1 Replies

6. Shell Programming and Scripting

exec script error

Hi, Gurus, my script code as following: #!/usr/bin/sh mkdir dir1 result=`echo $?` if ; then echo "completed" else echo "wrong" fi When I execute it with command sh filename. , it executed successfully. but, when I execute it with command . filename it throw out error: -bash:ELF :... (2 Replies)
Discussion started by: ken002
2 Replies

7. Shell Programming and Scripting

-exec cmd in ksh script

Hi, I discovered the following single-line script works very well to cp a large number of files from a source directory to a destination directory while avoiding the "argument list too large" error: # cpmany - copy large number of files # Takes two parameters - source dir, destination dir... (14 Replies)
Discussion started by: Tanuka
14 Replies

8. Shell Programming and Scripting

bash shell: 'exec', 'eval', 'source' - looking for help to understand

Hi, experts. Whould anybody clear explay me difference and usage of these 3 commands (particulary in bash) : exec eval source I've tryed to read the manual pages but did not get much. Also could not get something useful from Google search - just so much and so not exactly, that is... (3 Replies)
Discussion started by: alex_5161
3 Replies

9. Shell Programming and Scripting

using -exec in a script

I am using a third party job management program called Autosys. the command to load a jil into the autosys database is jil < somefilename.jil I have a directory and it in are a lot of jils. rather than type jil < somefilename.jil for every file I would like to script something do do it. if cd... (2 Replies)
Discussion started by: jayjabour
2 Replies

10. UNIX for Dummies Questions & Answers

Difference between xargs and exec

Hi, I have tried both the options in small dummy scripts, but somehow i can't differentiate between the two. find . -name H* -exec ls -l {} \; find . -name H* | xargs ls -l Both work the ditto way. Any help is appreciated. (19 Replies)
Discussion started by: vibhor_agarwali
19 Replies
Login or Register to Ask a Question