Common commands-eval analysis 1. working Principle and usage www.2cto.com usage: eval command-line principle: eval is mainly used in the special processing of parameters. In general command lines, shell can only process parameters once, such as conversion and variable transformation; but after adding eval, the parameter can be processed twice; on the Internet, it is said that it is unreasonable to process command-line twice. An eval can only be used by shell to process parameters once. Therefore, you can add several more evals, that is, eval command-line, so that you can compile the parameters three times, however, you should pay special attention to the escape of parameters. The following is an example. The eval command calculates (evalue) its parameters. These parameters are combined into a string after being calculated as expressions, and then executed as a command. The most common use of eval is to calculate and execute dynamically generated command lines. Example: $ name = woodie $ cmd = "echo Helllo $ name \! "$ Eval $ cmdHello woodie! 2. example 1: (Network download) pipe = "|" eval ls $ pipe wc-lshell replaces the pipe value when scanning command lines for 1st times. |, then run eval to scan the command line again, and shell uses | as the pipe symbol. You can use eval if the variable contains any characters that require shell to be seen directly in the command line (not the replacement result. The command line terminator (; | &), I/o redirection character (<>), and quotation marks are symbols of special significance to shell and must appear directly in the command line. X = 100 ptrx = xeval echo \ $ ptrx points to ptrx. here we can use the method to understand example 100 in B to print 100 eval $ ptrx = 50 and save 50 to the variable pointed to by ptrx. Echo $ x50 print 50 Example 2: root @ localhost :~ /Test # set tao shou kun root @ localhost :~ /Test # echo \ $ # $ 3root @ localhost :~ /Test # eval echo \ $ # kunroot @ localhost :~ /Test # Example 3: root @ localhost :~ /Test # aabbcc = alibabaroot @ localhost :~ /Test # a = export oot @ localhost :~ /Test # B = bbroot @ localhost :~ /Test # c = ccroot @ localhost :~ /Test # alibaba = "hello world" root @ localhost :~ /Test # eval echo \\ $ a $ B $ c11990aabbccroot @ localhost :~ /Test # eval echo \\\ \$ a $ B $ c $ alibabaroot @ localhost :~ /Test # eval echo \\ $ a $ B $ c11990aabbccroot @ localhost :~ /Test # eval echo \\\ \$ $ a $ B $ chello world Pay Attention to the parameter escape here. After the first processing, the parameter should be \$ $ aabbcc, the second time is: $ alibaba, and the third time is exactly: hello world. Appendix eval usage Example 3 (network collection): Example 1: # Find the variable name of the combination condition, then assign the value of this variable to another variable v1 = aaav2 = bbbc = 1if [$ c-eq 1] thenvname = v $ c # Find the variable named v1eval vvv =" $ "$ vname; echo vvv: $ vvv # assign the value of the variable v1 to vvv, that is, assign vvv = aaaeval vvv = '$ vname; echo vvv: $ vvv # assign the value of the variable v1 to vvv, that is, to vvv = aaa # eval vvv =$ $ vname; echo vvv: $ vvv # incorrect usage fi # Example 2: # Use aaa as the variable name as the value of v1, and assign the value of vaaa to the newly defined variable aaav1 = aaa; vaaa = "This is aaa" # eval $ v1 = $ vaaa; echo aaa: $ aaa # incorrect usage # eval $ v1 = "$ vaaa"; echo aaa: $ aaa # incorrect usage eval $ v1 = '$ vaaa'; echo aaa: $ aaa # Example 3: # Use aaa as the variable name, assign the variable name string to itself v1 = aaa; vaaa = "This is aaa" eval $ v1 = $ v1; echo aaa: $ aaa # The correct eval $ v1 = "$ v1"; echo aaa: $ aaa # is different from the error in example 2, this is the correct eval $ v1 = '$ v1'; echo aaa: $ aaa