The use of parentheses inside the shell

Source: Internet
Author: User
Tags logical operators


Middle Bracket

Brackets are divided into single brackets [], and double brackets [[]], and brackets are commonly used in expression judgments, and parentheses can also be used in expressions, as mentioned later.

Single brackets []

a=5b=3[$A-eq $B] determine if A is equal to b[${a}-eq ${b}] ["${a}"-eq "${b}" function as above
a=abc[$A = = "abc"] or write [${a} = = "abc"] or write ["${a}" = = "abc"] [$A \> "ABC"]
[-D./aaa.sh-a-E./bbb] Tests whether the aaa.sh is a directory and if BBB exists, returns true if all matches [-D./aaa.sh-o-E./bbb] Tests if aaa.sh is a directory and BBB exists, and returns if a condition is met Really

We can see from the above that there are some rules for using single brackets:

  • There should be a space between the variable and the bracket

  • There must be a space between the variable and the operator.

  • Variables can be referenced directly with $, or can be referenced using ${} and "${}", although it is possible to use the ${} directly, but it is recommended to refer to the variable using "${}" or directly with $

  • strings are enclosed in double quotation marks.

  • String comparisons generally use = = to do the same test as strings, use < or > to compare sizes, and to compare characters greater than and less than in single brackets by transferring \> and \<. The comparison principle is comparison of ASCII.

  • Logical operators such as-A and-O can also be used in single brackets


double brackets [[]]:

a=5b=3[[$A-eq $B]] "Determine if A is equal to b[[${a}-eq ${b}]] [[[" ${a} "-eq" ${b} "] as in the same role
a=abc[[$A = = "abc"] or write [[${a} = = "ABC]] or write [[" ${a} "= =" abc "]] or write [[$A = = ABC]][[$A > ABC]]
[[-D./aaa.sh &&-E./bbb]] Tests whether the aaa.sh is a directory and if BBB exists, returns true if all matches [[-D./aaa.sh | |-E./BBB]] Testing whether aaa.sh is a directory and BBB exists, and a condition is met to return true
a=abc[[$A =~ "a"]] to do pattern matching, this expression is true or written as [[${a} =~ "a"] or written as [["$[a]" =~ "A"]][[$A = A *]] expression is true [[$A = b*]] Expression is False
    • The use of double brackets is similar to the rules for using single brackets

    • The greater than less comparison of the middle string between the two brackets does not need to be escaped, and the string can be referenced without double quotes

    • Double brackets inside can use &&, | | Do logical operators

    • A fuzzy match can also be done inside the double brackets, using the =~ expression 2 can be a regular expression

[Expression 1-a expression 2] is equivalent to [[Expression 1 && expression 2]] equivalent to [expression 1] && [expression 2] equivalent to [[Expression 1]] && [[Expression 2]]
[Expression 1-o-expression 2] is equivalent to [[Expression 1 | | expression 2]] equivalent to [expression 1] | | [Expression 2] is equivalent to [[expression 1]] | | [[Expression 2]]


Parentheses

Single parenthesis

Single parentheses are used to represent the instruction group, which executes the command in parentheses sequentially through the child shell.

(ls./;echo "Hello"; mkdir-p/tmp/ccc)

The most common usage this is used in command substitution, command substitution can also use anti-quote ' '

echo "Current directory is: $ (PWD)" echo "current directory is: ' pwd '


Double parenthesis

It is commonly used in arithmetic operations and in the For loop.

a=$ ((5+3)) b=$ (($A +4))


The form of the For statement

1 to 10 loop execution

Notation 1:for i in (1..10);d odone notation 2:for i in SEQ 1 10;dodone notation 3:for ((i=1; i<=10; i++)); Dodone


The form of a while statement

1 to 10 cycles

I=1while [$I-le];d o let I++done


This article is from the "Little Demon's Home" blog, so be sure to keep this source http://littledevil.blog.51cto.com/9445436/1875711

The use of parentheses inside 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.