![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| eval in shell scripting | mobydick | Shell Programming and Scripting | 10 | 06-25-2009 02:24 PM |
| exit status of eval exec | robotball | Shell Programming and Scripting | 1 | 06-06-2008 09:46 PM |
| eval in bash | jetfreggel | Shell Programming and Scripting | 3 | 01-02-2008 05:39 PM |
| Bash Source Code | bhargava | Shell Programming and Scripting | 1 | 11-20-2006 03:45 PM |
| difference between source, exec and ./script | 98_1LE | UNIX for Dummies Questions & Answers | 1 | 04-15-2001 09:24 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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
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." 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" |
|
||||
|
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> |
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Tags |
| bash, bash eval, eval |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|