The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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
bash shell: 'exec', 'eval', 'source' - looking for help to understand alex_5161 Shell Programming and Scripting 3 07-24-2008 12:42 PM
c shell scripting bigboizvince Shell Programming and Scripting 1 03-25-2008 09:33 AM
Shell scripting sreejith SUN Solaris 2 10-08-2006 09:38 AM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 08:12 AM
shell scripting anuradr Shell Programming and Scripting 1 03-08-2006 06:22 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-21-2008
mobydick mobydick is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 41
eval in shell scripting

what is the real usage of eval in shell scripting

whats the difference between
$ cmd

&&
eval cmd
  #2 (permalink)  
Old 05-21-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
eval is useful when cmd contains something which needs to be evaluated by the shell. The typical case would be when you have a variable containing the index of an argument you want to access.

Code:
n=1  # or whatever, just to make this self-contained
eval echo \${$n}
Search these forums for examples; some of them are rather elaborate.
  #3 (permalink)  
Old 05-21-2008
mobydick mobydick is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 41
but whats the difference if i write

echo $($n) instead of

eval echo \${$n}
  #4 (permalink)  
Old 05-21-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Experiment to see.

Code:
vnix$ n=1
vnix$ echo $($n)
bash: 1: command not found
vnix$ n="date"
vnix$ echo $($n)
Wed May 21 20:39:00 EEST 2008
So it uses the value of $n as a command. (It's the same as echo `$n` with backticks.)

Contrast:

Code:
vnix$ set -- one two three  # sets $1 $2 $3
vnix$ echo $1
one
vnix$ n=1
vnix$ echo ${$n}  # attempt to echo $1
bash: ${$n}: bad substitution
vnix$ eval echo \${$n}
one
See? Now if you assign n=2 it will echo the value of $2, etc. So you can change the name of the variable you are referring to programmatically, dynamically.

This is an advanced topic; if you don't have a use for it, don't bother. If you really want to understand this, I suggest you play around with set -x and try different things until you understand what's going on.
  #5 (permalink)  
Old 05-22-2008
mobydick mobydick is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 41
thanks its convincing and clear ..

thanks for the help
  #6 (permalink)  
Old 05-22-2008
mobydick mobydick is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 41
querry..including scripts into another script

is there any way to include or import one script into other
like application programs
  #7 (permalink)  
Old 05-22-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
Include as in..? You can call one script from the other. That can work.
Reply

Bookmarks

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0