Example of eval usage in the shell _linux shell

Source: Internet
Author: User
Tags eval

Function Description: Tell the shell to remove the eval parameters, recalculate the contents of the parameters.
Syntax: eval [parameters]
Supplemental Note: Eval can read a series of parameters and then perform them according to the characteristics of the parameters themselves.
Parameters: Unlimited number of arguments, separated by semicolons.
1. Scan it two times before executing the command line, and then calculate the contents of the parameter again.

Copy Code code as follows:

[root@localhost blue]# a= "ls |more"
[Root@localhost blue]# $a #ls

Will | And more as a parameter, rather than a file as a page display
Copy Code code as follows:

Ls:cannot access |more:no such file or directory
[Root@localhost blue]# eval $a #取出

Their parameters and execute them again
Copy Code code as follows:

1
1.cpp
2
2.cpp
3
3.cpp
[Root@localhost test]# a= "123"
[Root@localhost test]# Echo ' ${' "A" '} '
${a}
[Root@localhost test]# eval echo ' ${' "A ' '} ' #再次执行 ${a} command
123

2.eval get last parameter
Copy Code code as follows:

[Root@localhost blue]# Echo ' eval echo \$$# ' > Last
[Root@localhost blue]# Cat Last
Eval echo \$$#
[Root@localhost blue]#./last 1 2 3 4
4

Additional advanced usage is added later!
Eval is a flexible application of the bash shell command-line processing rules and constructs the "smart" command to implement complex functionality.
The command mentioned above is one of the most common applications of Eval, which repeats 1 command-line parameter passes and simply executes commands.
It's a hard nut to bash and a compulsory skill for advanced bash programmers.

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.