Home > php教程 > PHP开发 > How to use parentheses in Shell

How to use parentheses in Shell

高洛峰
Release: 2016-11-23 17:41:11
Original
1373 people have browsed it

Brackets

Brackets are divided into single brackets [] and double brackets [[]]. Square brackets are generally used in expression judgment, and parentheses can also be used in expressions, which will be mentioned later.

Single square brackets []

A=5
B=3
[ $A -eq $B ]    判断A是否等于B
[ ${A} -eq ${B} ]  作用同上
[ "${A}" -eq "${B}" ]  作用同上
Copy after login

     

A=ABC
[ $A == "ABC" ]   或写成  [ ${A} == "ABC" ]   或写成  [ "${A}" == "ABC" ]
[ $A \> "ABC" ]
Copy after login

   

[ -d ./aaa.sh -a -e ./bbb ]  测试aaa.sh是否为目录并且bbb是否存在,如果都符合则返回真
[ -d ./aaa.sh -o -e ./bbb ]  测试aaa.sh是否为目录并且bbb是否存在,满足一个条件则返回真
Copy after login

 

We can see some rules for using single square brackets from the above:

There must be a space between the variable and the square bracket

variable There must also be a space between the operator and the operator

Variables can be referenced directly with $, or they can be referenced with ${} and "${}". Although ${} can be used directly, it is recommended to use "${} ", or use $ directly to quote variables

Strings should be enclosed in double quotes

String comparison generally uses == to test whether the strings are the same, and use < or > for size comparison, in a single To compare characters in parentheses for greater than and less than, use transfer > and <. The comparison principle is to compare ASCII.

In addition, you can also use logical operators like -a and -o in single square brackets


Double square brackets [[]]:

A=5
B=3
[[ $A -eq $B ]]    判断A是否等于B
[[ ${A} -eq ${B} ]]  作用同上
[[ "${A}" -eq "${B}" ]]  作用同上
Copy after login

                                                                   

A=ABC
[[ $A == "ABC" ]]   或写成  [[ ${A} == "ABC" ]]   或写成  [[ "${A}" == "ABC" ]] 或写成 [[ $A == ABC ]]
[[ $A > "ABC" ]]
Copy after login

The rules for using double square brackets are similar to the rules for using single square brackets

The greater-than-less comparison of strings between double square brackets does not need to be escaped, and the strings can be quoted without double quotes

Can be used inside double square brackets &&, || are used as logical operators

You can also do fuzzy matching inside double brackets, use =~Expression 2 can be a regular expression

[[ -d ./aaa.sh && -e ./bbb ]]  测试aaa.sh是否为目录并且bbb是否存在,如果都符合则返回真
[[ -d ./aaa.sh || -e ./bbb ]]  测试aaa.sh是否为目录并且bbb是否存在,满足一个条件则返回真
Copy after login

A=ABC
[[ $A =~ "A" ]]   做模式匹配,这个表达式为真
或写成  [[ ${A} =~ "A" ]]  或写成  [[ "$[A]" =~ "A" ]]
[[ $A = A* ]] 表达式为真  [[ $A = B* ]] 表达式为假
Copy after login

Single brackets

Brackets

Single parentheses used alone represent a command group, and the commands in the brackets will be executed sequentially through a subshell

[ 表达式1 -a 表达式2 ] 等效于 [[ 表达式1 && 表达式2 ]] 等效于 [ 表达式1 ] && [ 表达式2 ] 等效于 [[ 表达式1 ]] && [[ 表达式2 ]]
Copy after login

The most common usage is to use it in command substitution. Backticks can also be used for command substitution. ``

[ 表达式1 -o 表达式2 ] 等效于 [[ 表达式1 || 表达式2 ]] 等效于 [ 表达式1 ] || [ 表达式2 ] 等效于 [[ 表达式1 ]] || [[ 表达式2 ]]
Copy after login

Double parentheses

are commonly used in arithmetic operations and also used in for loops

(ls ./;echo "hello";mkdir -p /tmp/ccc)
Copy after login

For statement

1 to 10 Loop execution

echo "当前目录是:$(pwd)"
echo "当前目录是:`pwd`"
Copy after login

While statement form

1 to 10 loop

A=$((5+3))
B=$(($A+4))
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template