BASH shell test condition tests [[]] and [] similarities and differences summary

Source: Internet
Author: User

The test command is very powerful, but it is difficult to satisfy its escape requirements and the difference between string and arithmetic comparisons.



Greater than Less than Equals Not equal to
If ["$a"-gt "$b"] If ["$a"-lt "$b"] If ["$a"-eq "$b"] If ["$a"-ne "$b"]
if [["$a"-gt "$b"]] if [["$a"-lt "$b"]] if [["$a"-eq "$b"]] if [["$a"-ne "$b"]]
if (("$a" > "$b")) if (("$a" < "$b")) if (("$a" = "$b")) if (("$a"!= "$b"))


String comparisons

Greater than Less than Equals Not equal to
If ["$a" \> "$b"] If ["$a" \< "$b"] If ["$a" = "$b"]
If ["$a" = "$b"]
If ["$a"!= "$b"]
if [["$a" > "$b"]] if [["$a" < "$b"]] if [["$a" = "$b"]]
if [["$a" = "$b"]]
if [["$a"!= "$b"]]


[[]] and []
Function [[ ]] [ ] Example
String comparisons > \> -
= (or = =) = -
!= != -
An expression combination && -A [[-N $var &&-F $var]] && echo "$var is a file"
|| -O -


Pattern matching
(Pattern matching)


= (or = =) Wood has [[$name = "A *"]]-> the string "A *"
Regular match (regularexpression matching) =~ Wood has [[$ (date) =~ ^fri\]] && echo "It s Friday the 13th!"


-nt/-ot

Attribute

 

Example

File or folder exists

-e

[[ -e  $config &NBSP;]]&NBSP;&&&N bsp;echo  "config file exists:  $config"

File New and old comparison

[[  $file 0 -nt  $file 1 ]] && echo  "$file 0  is newer than  $file 1″

Same file

-ef

td>

[[  $input  -ef  $output  ]] \ && { echo  "will not& nbsp;overwrite input file:  $input "; exit 1; } 

Negative

!

-

Some examples of


Logical combination judgments:
If [$condition 1] && [$condition 2]
If [$condition 1-a $condition 2]
If [$condi Tion1 && $condition 2]]
If [$condition 1] | | [$condition 2]
If [$condition 1-o $condition 2]
If [[$condition 1 | | | $condition 2]] # Also works.
#The &&, | | |, operators work within a [[]] test, despite giving an error within a [] construct.

Greater than Less than Equals Not equal to
If ["$a"-gt "$b"] If ["$a"-lt "$b"] If ["$a"-eq "$b"] If ["$a"-ne "$b"]
if [["$a"-gt "$b"]] if [["$a"-lt "$b"]] if [["$a"-eq "$b"]] if [["$a"-ne "$b"]]
if (("$a" > "$b")) if (("$a" < "$b")) if (("$a" = "$b")) if (("$a"!= "$b"))


String comparisons

Greater than Less than Equals Not equal to
If ["$a" \> "$b"] If ["$a" \< "$b"] If ["$a" = "$b"]
If ["$a" = "$b"]
If ["$a"!= "$b"]
if [["$a" > "$b"]] if [["$a" < "$b"]] if [["$a" = "$b"]]
if [["$a" = "$b"]]
if [["$a"!= "$b"]]


[[]] and []
Function [[ ]] [ ] Example
String comparisons > \> -
= (or = =) = -
!= != -
An expression combination && -A [[-N $var &&-F $var]] && echo "$var is a file"
|| -O -


Pattern matching
(Pattern matching)


= (or = =) Wood has [[$name = "A *"]]-> the string "A *"
Regular match (regularexpression matching) =~ Wood has [[$ (date) =~ ^fri\]] && echo "It s Friday the 13th!"


attribute

 

Example /strong>

File or folder exists

E

[ -e  ; $config  ]] && echo  "config file exists:  $config"

File old and new comparison

-nt/-ot

[[  $file 0 -nt  $file 1 ]] && echo  "$file 0 is newer  than  $file 1″

Same file

-ef

[[   $input  -ef  $output  ]] \ && { echo  "will not  overwrite input file:  $input "; exit 1; } 

Deny

!

-

Some examples of


logical combination judgments:
If [$condition 1] && [$condition 2]
If [$condition 1-a $condition 2 ]
If [$condition 1 && $condition 2]]
If [$condition 1] | | [$condition 2]
If [$condition 1-o $condition 2]
If [[$condition 1 | | | $condition 2]] # Also works.
#The &&, | | |, operators work within a [[]] test, despite giving an error within a [] construct.

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.