LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2010, 11:59 PM   #1
daudiam
Member
 
Registered: Mar 2010
Posts: 68

Rep: Reputation: 15
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?
 
Old 06-20-2010, 01:11 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well your initial error is to do with quoting. Using full quoting, ie single quote, tells the shell not to expand special charachters.
Therefore, your pipe is now a character and no longer has its special meaning.

eval on the other hand takes a string as argument and now evaluates the entire string as if you were at the command line, hence the command now works
as the pipe is now a redirect.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] what is effect of mail command sumeet inani Linux - Newbie 11 12-19-2009 03:27 AM
Only execute 2nd command on successufl execution of 1st command kregec05 Linux - Newbie 3 08-19-2009 10:29 AM
Execute a command , reboot and then execute another command ganeshp@moris.org Linux - Newbie 3 12-03-2008 12:51 AM
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 06:31 AM
No effect when 'export' command executed in a script? sylvain_gnu Linux - Software 6 04-20-2004 07:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration