Talking about Shell-eval

Source: Internet
Author: User

First, the basic use of Eval

Eval will scan the back of the CmdLine two times, if after the first scan, cmdline is a normal command, then execute this command;

If the cmdline contains an indirect reference to a variable, the semantics of the indirect reference is guaranteed.

The 1.eval command will first scan the command line for all substitutions before executing the command. This command is used for variables that do not perform its function at one time, and the variable is scanned two times. Variables that need to be scanned two times are called complex variables.

2.eval can also be used to echo simple variables, not necessarily complex variables.

3. Two scans.

Example: test.txt content: Hello Shell world!

Myfile= "Cat Test.txt"

(1) Echo $myfile #result: Cat test.txt

(2) eval echo $myfile #result: Hello Shell world!

From (2) You can tell that the first scan was replaced by a variable, and the second scan executed the command contained in the string.

4. Obtain the last parameter.

Example: Set 11 22 33 44

If you want to output the most recent parameter, which is 44, you can use the following command,

Echo $4

But if we do not know a few parameters, to output the last parameter, you might think of using $# to output the last parameter.

If you use the command:

echo "\$$#"

The result is $4, not the 44 we want.

Here comes the question of an indirect reference to a variable, our intention is to output 44, by default, after the command ignores the case of the variable indirect reference.

Use the eval command to reach our expectations:

eval echo "\$$#"

The resulting result is 44.


Ii. the difference between the Shell's Eval and other languages

The shell also provides the Eval command, which takes its arguments as command execution, and at first glance doubts why the shell provides two mechanisms to dynamically execute the command string, but after careful analysis, it is found that the Shell's eval differs greatly from other languages.

Functions in 1.shell can be returned by return, but return is equivalent to exit, which is only a state value for testing, rather than returning complex results in the same way as other languages, whose processing results can only be obtained through the output to standard output through "and $ ().

Eval in 2.shell

A. Unable to get the result of the function processing, as 1 says, all commands, the result of the function can only be obtained by ", then the use of Eval in other languages to obtain the output of dynamically generated code is impossible.

B.eval nesting is meaningless, in other languages it can be done by eval (eval ("Code")), but because eval in the shell executes the subsequent eval command as a command string without nesting, nesting is ordered to be Replacement.


This article is from "GREEN" blog, please make sure to keep this source http://green906.blog.51cto.com/10697569/1788442

Talking about Shell-eval

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.