Shell----', $ (), [[...] usage of]],[...],eval

Source: Internet
Author: User
Tags echo command

The difference and relation between the inverted quotation mark "" "and the $ ()

' $ ' (parameter substitution) and ' ' (Command substitution)

The use of the anti-quote "'" and $ () is the same. When executing a command, the statement in the ", or $ () is executed once as a command, and the result is added to the original command for re-execution


For example:
echo ' ls ' #会先执行 ls to get xx.sh and so on, then replace the original command as:
Echo xx.sh
The result of the final execution is: xx.sh
usually we come across the output of the execution of a bunch of commands into a variable, which needs to be enclosed in this command substitution, butAccording to the POSIX specification, system engineers are required to use$ (command)The form. Therefore, we'd better follow this specification, less ", more than $ ().


Ii. the difference and connection between [[...]] and [...]

1.[...]

1) single bracket "[" is an executable program , the path is "/usr/bin/[", is the internal command of bash. It is equivalent to the executable program "test".

If we don't specify the absolute path, we usually use Bash's own commands. The left bracket in the if/test structure is the command ID that calls test, and the right bracket is judged by the close condition . This command takes its arguments as a comparison expression or as a file test, and returns an exit status code based on the results of the comparison. The if/test structure does not have to be in the right bracket, but this is required in the new bash.

2) The comparison operators available in test and [] are only = = and! = , both for string comparisons , not for integer comparisons, and for integer comparisons only with-eq. -gt this form. The greater than sign is not supported either for string comparisons or for integer comparisons. If you really want to use, you can use the escape form for string comparisons, if you compare "AB" and "BC": [AB \< BC], the result is true, that is, the return status is 0. logic and logic in [] or using-A and-O are represented .

3) Character range. Used as part of a regular expression to describe a matching range of characters. The regular is not used within brackets as a test purpose.

4) in the context of an array structure , brackets are used to refer to the number of each element in the array .


2.[[...]]

1) Double brackets "[[] is a keyword for the script interpreter (BASH,SH, etc.) and is not a command. [[]] structure is more general than [] structure. There is no filename extension or word splitting between all characters in [[and]], but parameter extensions and command substitution occur.

2) supports pattern matching of strings, and even supports shell regular expressions when using the =~ operator. String comparisons can be made to the right as a pattern, not just a string, such as [[Hello = = Hell]], the result is true. matches a string or wildcard character in [[]] without the need for quotation marks.

3) use [[...]] The conditional judgment structure, rather than [...], can prevent many logic errors in the script. For example,,&&, | |, <, and > operators can normally exist in the [[]] conditional judgment structure, but if they appear in the [] structure, an error will be found.

4) Bash considers expressions in double brackets as a single element and returns an exit status code .


Iii. usage ofeval

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

1. Eval command-line

Eval is mainly used in the special handling of parameters above, the General Command line, shell processing parameters are only performed once, like escape and variable transformation, but with the eval can be done two times the parameters of processing. An eval can only allow the shell to handle the parameters one more time, so there are a few eval can be added several times, that is, eval eval command-line so that the parameters can be compiled three times, but this time should pay special attention to the escape of parameters, the following examples are explained.

The most common use of eval is to compute and execute dynamically generated command lines. The eval command computes (evalue) its arguments, which are evaluated as expressions and then re-assembled into a string and executed as a command.

Pipe= "|" Eval ls $pipe wc-l

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/82/C2/wKiom1dgBDOxZavUAAAzCSnEBVk426.jpg "title=" 1.jpg " Style= "Float:none;" alt= "wkiom1dgbdoxzavuaaazcsnebvk426.jpg"/>

When the shell scans the command line for the 1th time, it replaces the pipe's value |, and then eval causes it to scan the command lines again, when the shell takes | as the pipe symbol.

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

(1)

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/82/C0/wKioL1dgBUbzBgj9AABKjmLHZVg641.jpg "style=" float: none; "title=" 2.jpg "alt=" Wkiol1dgbubzbgj9aabkjmlhzvg641.jpg "/>

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

(2) Support for Sanchong

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/82/C2/wKiom1dgBlfwmoVYAADH8uqtBZA199.jpg "title=" 2-2. JPG "alt=" wkiom1dgblfwmovyaadh8uqtbza199.jpg "/>

watch this. parameter Escaping , the first time after processing the parameters should be: \$ $AABBCC, the second is: $alibaba, the third is exactly the same: Hello World.

with eval command to create a pointer to a variable:

X=100ptrx=xeval echo \$ $ptrx #指向ptrx100 #打印100eval $ptrx = 50 # Save 50 to the variable that Ptrx points to. echo $x #打印50

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/82/C2/wKiom1dgBT3xNHdnAABeULeZPK8943.jpg "title=" 3-1. JPG "alt=" wkiom1dgbt3xnhdnaabeulezpk8943.jpg "/>









This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1789311

Shell----', $ (), [[...] usage of]],[...],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.