The eval command in the shell

Source: Internet
Author: User

In Shell learning, we encounter these two symbols: anti-quote (') and $ (), what are the differences between them?

We all know that in bash, anti-quotes and $ () are used for command substitution, and command substitution is used to reorganize the command line, complete the command in quotation marks, and then replace the result with the new command line. That is, when executing a command, the statement in the ", or $ () is now executed once as a command, and the result is added to the original command for re-execution, for example:

650) this.width=650, "title=" 8{k_cqlf{@DPT $QN%n) 3]d6.png "src=" http://s2.51cto.com/wyfs02/M00/82/A5/ Wkiol1dehv6xpo9oaaalhy606g4859.png "alt=" Wkiol1dehv6xpo9oaaalhy606g4859.png "/>

From the above we can see that when we echo the LS command inside the anti-quote, and we directly execute the LS command is the same, and in the right place we said before, that is, when executing a command, it will now be in the ", or a $ () statement as a command to execute again, The result is then added to the original command for re-execution.

Although in operation, both of these can achieve the same effect, but in peacetime use, we try to use $ (), because:

First of all, the anti-quote ' and single quote ' is likely to be confusing in use, causing unnecessary trouble for us

Second, in a multi-layered compound substitution, the anti-quote ' ' needs to have an extra hop-off (backslash), while $ () is more intuitive;

But the drawback of $ () is that not all UNIX systems support this approach, but the anti-quotes are definitely supported.


eval Command usage:

When we add eval to the command line, the shell scans it two times before executing the command. The eval command will first scan the command line for all permutations before executing the command. This command applies to variables that scan for a time that does not function. This command scans the variable two times.

    1. Eval can be used to echo simple variables:

650) this.width=650; "title=" TU3_HK4SWB "[7kbl_}3vs5q.png" src= "http://s5.51cto.com/wyfs02/M01/82/A5/ Wkiol1dejs6wuaijaaaaydakv5i781.png "alt=" Wkiol1dejs6wuaijaaaaydakv5i781.png "/>

This is the same as the normal command without the eval keyword.


2.eval can also be used to execute commands that contain strings:

First we create a file, enter some text we want to output in this file, and then we assign the cat file to the variable myfile, the whole process is as follows:

650) this.width=650; "title=" P9os[~zi ' 3unpxi]lw{rz5a.png "src=" http://s3.51cto.com/wyfs02/M01/82/A5/ Wkiol1dejuig33v2aaa5crwaaci566.png "alt=" Wkiol1dejuig33v2aaa5crwaaci566.png "/>

From the above we can see that when we give the cat file directly to the myfile variable, the contents of our direct echo myfile will not output our text in the file. Then, when we use the eval command, we will output what we want. Because the eval command can not only displace the variable, it can also execute the corresponding command. In the first scan, the variable is replaced, and the second scan executes the cat file command inside the string contained in the variable, so it outputs what we want.


The 3.eval command can also display the last parameter we passed to the script:

We now have one of the following scripts:

650) this.width=650; "Title=" CE8 (R (W{i (0qc9tsgf9s9.png "src=" http://s2.51cto.com/wyfs02/M00/82/A8/ Wkiom1dekeeiz7v6aaapkvalsy4951.png "alt=" Wkiom1dekeeiz7v6aaapkvalsy4951.png "/>

We have the following execution procedures:

650) this.width=650; "title=" Z7tt ' OTUL5 "A@N8032FLSHG.png" src= "http://s5.51cto.com/wyfs02/M00/82/A8/wKiom1deKL_ Jf0q0aaaw7tqayvk986.png "alt=" Wkiom1dekl_jf0q0aaaw7tqayvk986.png "/>

From the above results, we can see that when we execute the script, the result is always the last parameter we entered, what is going on? This is the use of eval, in the above script, the eval command first put $$ #解析为当前shell的参数个数 (we are here 4), and then the second scan to get the last parameter.


The 4.eval command can also create pointers to variables:

650) this.width=650; "title=" uz2c%q6b~kldzn@%${{x) ei.png "src=" http://s5.51cto.com/wyfs02/M01/82/A6/ Wkiol1deleigxithaaaggtvmg00244.png "alt=" Wkiol1deleigxithaaaggtvmg00244.png "/>

In the above command, we define a variable val=100, and then we define a PTR and assign Val to it. Then we use the eval command to parse the value of PTR, and we can see that it is 100. Then we change the Val value by PTR, and then we look at the Val value and it becomes 50.






















The eval command in the shell

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.