bash shell: 'exec', 'eval', 'source' - looking for help to understand | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Tags
bash, bash eval, eval

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-22-2008
alex_5161 alex_5161 is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 6 November 2014, 10:43 AM EST
Location: Detroit
Posts: 184
Thanks: 18
Thanked 2 Times in 2 Posts
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 is not usefull
Would it be any different result in environment after using any of these command?
Where and for what purpose each should be used?
How and is there any difference to use those from interactive shell or from script?

Greately appreciate your help
Sponsored Links
    #2  
Old 02-22-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 18 December 2014, 7:21 AM EST
Location: NM
Posts: 10,310
Thanks: 295
Thanked 821 Times in 763 Posts
eval "translates" a value buried inside a variable, and then runs the command that was buried in there

Code:
for i in 1 2 3
do
   eval myvar="$i"
   echo "$myvar"
done
# this gives 
1
2
3
# why? because there is no metavalue or special meaning to 1 or 2 or 3


Code:
for i in ls df
do
    eval myvar="$i"
    echo "$myvar"
done
# here you get output from the ls command and the df command

exec starts another process - BUT - it exits the current process when you do this kind of thing


Code:
#!/bin/bash

exec echo "leaving this script forever  $0"   # Exit from script here.

# ----------------------------------
# The following line never happens

echo "This echo will never echo."

source

When you run a command in the shell - like another script or a command like ls -
the shell creates a subprocess (called child process). Any environment variable that got defined or changed down in the child is LOST FOREVER to the parent process.

However if you source a script (there are two ways) you force the script to run in the current process. That means environment variables in the script you ran are NOT LOST.


Code:
# script1
MYVAR="HI there!"
# end script1


Code:
# script2
# first, run script 1
script1
# now is the MYVAR variable set?  No -why? it got set in the child, when the child exited it GOT LOST
echo "MYVAR= $MYVAR"
# now source script1 :: putting a lonesome dot or the command  "source" 
#  in front of the script name have the same effect
. script1
#  . script1 == source script1  these two are the SAME THING
source script1
# now is the MYVAR variable set?  This time we see that MYVAR has a value
echo "MYVAR= $MYVAR"

The Following User Says Thank You to jim mcnamara For This Useful Post:
Bill L. (06-20-2013)
Sponsored Links
    #3  
Old 02-22-2008
alex_5161 alex_5161 is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 6 November 2014, 10:43 AM EST
Location: Detroit
Posts: 184
Thanks: 18
Thanked 2 Times in 2 Posts
jim mcnamara -
thank you, very much, everything clear and usefull for me now!

I am realy exiting to understand it. Now I have new (for me ) possibility in scripting and in shell.
Definetely, 'eval' gives usefull opportunity!
The 'exec' - nice to know that and it is good to use it, too.
The 'sorce' - it appeared that I have used it already for long time (as '. script') but did not realized tha all definishions of the curent shell are available in runing script by that (other part - backward - usualy is a reason to use the '. scr')
I've just tryed it and maybe it would be usefull to show it here, too, although your explanation pretty enough, but, anyway, here it is:


Code:
bash-802:/home/dca0701/develop/src> cat > try.sh
/usr/bin/echo "\n -- try to use already defined in shell alias:"
ec "<<< this printed by shell alias 'ec'"

shopt -s expand_aliases
alias ec2=ec
ec "--- new alias is declared as ec2"
^C
bash-803:/home/dca0701/develop/src>
bash-803:/home/dca0701/develop/src> chmod 777 try.sh
bash-804:/home/dca0701/develop/src>
bash-804:/home/dca0701/develop/src>
bash-804:/home/dca0701/develop/src> try.sh

 -- try to use already defined in shell alias:
./try.sh: ec: command not found
./try.sh: ec: command not found
bash-805:/home/dca0701/develop/src>
bash-805:/home/dca0701/develop/src> source try.sh

 -- try to use already defined in shell alias:
<<< this printed by shell alias 'ec'
--- new alias is declared as ec2
bash-806:/home/dca0701/develop/src> ec2 hello
hello
bash-807:/home/dca0701/develop/src>

Once again, thank you very much!
    #4  
Old 07-24-2008
phorgan1 phorgan1 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 28 July 2008, 1:42 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
One thing that eval does that is critical is break things apart in strings. For example if in a script you wrote lscommand="ls -F", then tried to run $lscommand, the shell would give you an amusing error that it didn't find the command ls -F. If instead you eval $lscommand, it works fine. One place I use that a lot is in a script where some args are to be passed to a command (or commands). I gather them together in variable, such as lsoptions="$lsoptions $arg" At the end of dealing with the arguments, the lsoptions might have for example, "-F -a". If I try to run ls $lsoptions ls will complain about the argument "-F -a" (and you'll get really odd incomprehensible complaints too! If instead, I do eval ls $lsoptions it does just what I wanted. Yea!

Patrick
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help with this c++ source code! DOnt understand what some stuff mean. orszhak Programming 2 04-09-2011 02:00 PM
I need to understand the differences between the bash shell and the Bourne shell awk_sed_hello Shell Programming and Scripting 7 11-05-2009 05:40 AM
help me understand exec() family of calls... c_d Programming 4 03-29-2009 05:52 PM
Exec bash shell via PHP Site ! lamthenhan Shell Programming and Scripting 1 07-22-2008 08:17 AM
exit status of eval exec robotball Shell Programming and Scripting 1 06-06-2008 10:46 PM



All times are GMT -4. The time now is 11:22 AM.