Effect of using eval to execute a command as opposed to writing it on the commandline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Effect of using eval to execute a command as opposed to writing it on the commandline
# 1  
Old 06-20-2010
Effect of using eval to execute a command as opposed to writing it on the commandline

Code:
cmd='date | wc' or cmd="date | wc"
$cmd

If this script is executed, an error is generated. The reason written was that "The execution fails because the pipe is not expanded and is passed to date as an argument". What is meant by expansion of pipe. When we execute date | wc on the command line, it goes fine. then | is not treated as an argument. Why?

If I write
Code:
cmd='date | wc' or cmd="date | wc"
eval $cmd

It works. How?

I understand (hopefully) how eval works in 2 passes i.e. the command first gets evaluated and then executed.
# 2  
Old 06-20-2010
Quote:
Originally Posted by daudiam
Code:
cmd='date | wc' or cmd="date | wc"
$cmd

If this script is executed, an error is generated. The reason written was that "The execution fails because the pipe is not expanded and is passed to date as an argument". What is meant by expansion of pipe. When we execute date | wc on the command line, it goes fine. then | is not treated as an argument. Why?

If I write
Code:
cmd='date | wc' or cmd="date | wc"
eval $cmd

It works. How?

The shell has already tokenized the command line and performed any redirection before the variable is expanded.

When you use eval, the expansion is done first, then the shell parses the result.
This User Gave Thanks to cfajohnson For This Post:
# 3  
Old 06-21-2010
Thanks. I get that since all the special symbols like |,",',; all are evaluated before substitution takes place. Hence, in the case of $cmd, since no specia symbols were initially found, cmd was substituted for "date | wc" and | was taken as an argument to date (as | now has no meaning).

One question though, why do call evaluating the special symbols like | as expanding it? Does it mean that a special code for these symbols is inserted there when they're expanded ?

Also, it seems that there is another function of eval. When there are no substitutions to be made, then eval just removes the special symbols like '' and backslash. Like in the following :

eval echo \\l
eval echo \\\k
eval echo 'h'

I'm using bash shell

Last edited by daudiam; 06-21-2010 at 12:46 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Usage of '-' in eval command.

Hi, I am trying to use eval command to evaluate a variable(HAPROXY_LISTENER_rabbitmq_project-test-BRHM_PORT) which consists of '-' but unfortunately the eval command is unable to interpret the value of variable and trims the variable name after '-' and produces the string output rather than the... (10 Replies)
Discussion started by: Kuldip
10 Replies

2. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

3. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Solaris

prtdiag command works only from commandline manually

When i run the command sudo /usr/sbin/prtdiag manually on a solaris 10 box, it works as expected. But the same command from my java code gives me the following error. picl_initialize failed: Daemon not responding. I am unable to figure out why is not running from my code.:wall: (3 Replies)
Discussion started by: randeepsp
3 Replies

5. Shell Programming and Scripting

Help with perl eval command .....

Hi All, I read the above written code (perl code) in another perl script and evaluates this code for each line of text file,but using exit statement in code make this not to work and i could not get the desired results. However if i use return it works fine. I just need to know why it doesn't... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

7. Shell Programming and Scripting

Eval command help

I have file called myfile which has the text "myserver" in it. I need to have a command to ping "myserver". How would I do that? I tried when I type at the terminal I get the output as . How do I do something like a ? thanks, Nick (5 Replies)
Discussion started by: nikhilfake
5 Replies

8. Shell Programming and Scripting

assignment to variable from eval command

Hi Gurus, I am having 2 parameters as below parm1=value1 parm2=parm1 I want to evaluate parm1 value using eval echo \$$parm2 and later i want to assign this value to other variable which i will be using in if statement like : if ]; then do this....... fi could you please suggest... (5 Replies)
Discussion started by: k_vikash
5 Replies

9. Shell Programming and Scripting

Nullify the effect of Trap command in later part of the script

Hi All, i have an issue regarding trap command. i have specified trap function in the beginning of the script to catch some signals but in the later part of the script i want to remove the effect of this. Can anybody help me out of this. for e.g. pressing Ctrl+C for the first time should... (2 Replies)
Discussion started by: vikas_kesarwani
2 Replies

10. Shell Programming and Scripting

Access to database/eval command

Hi i have the following code: if(($line!=1) and (@field!='\$')){ print ( "\nTRY TO CONNECT TO DATABASE................\n"); my $dbh = DBI->connect($dsn, $user, $pass); print ("CONNECTED TO DATABASE\n"); eval ... (1 Reply)
Discussion started by: chriss_58
1 Replies
Login or Register to Ask a Question