The Eval usage of Linux (the required skill of the Advanced Bash Programmer)

Source: Internet
Author: User

1. Eval command-line

where command- line is a plain command that is typed on the terminal. But when you put it in front of it, Eval The result is that the shell scan it two times before executing the command line. such as:

Pipe= "|"

Eval ls $pipe wc-l

1 Scan command line, it replaces eval causes it to scan the command line again, shell takes | As a pipe symbol.

If the variable contains any required shell characters you see directly on the command line (not the result of a substitution), you can use the eval . command line terminator (;| &),I/o redirection (< > and quotation marks are symbols that have special meaning to the shell and must appear directly on the command line.

2. Eval echo \$$# to get the last parameter

such as:cat Last

Eval echo \$$#

./last one, three four

Four

After the first scan, theshell removes the backslash. When the shell scans the row again, it replaces the value of the $4 and executes the echo command

3. The following shows how to use the eval command to create a pointer to a variable:

x=100

Ptrx=x

eval echo \$ $ptrx point to Ptrx, use this method to understand the example in b

Print

eval $ptrx =50 will be stored in the variable that Ptrx points to.

Echo $x

Print

--------------------------------------------------------------------------------------------------------------- ------------------------------

The 1.eval command will first scan the command line for all substitutions, and then execute the command with a few honest commands. This command is used for variables that scan for a time that cannot implement its functionality. This command scans the variable two times. These variables, which need to be scanned two times, are sometimes referred to as complex variables.

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

Name=zone

The eval echo $NAME is equivalent to echo $NAME

3. Two scans

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. Get the last parameter

echo "Last argument is $ (eval echo \$$#)"

echo "Last argument is $ (eval echo $#)"

The shell also provides the Eval command, as well as the familiar scripting language, 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, and cannot return complex results as in other languages, and its processing results can only be obtained through the output to standard output through ', $ ().

Http://blog.chinaunix.net/uid-21411227-id-1826706.html

Http://blog.sina.com.cn/s/blog_a7b22c930101tgw6.html

The Eval usage of Linux (the required skill of the Advanced Bash Programmer)

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.