The basics of Shell programming [and the similarities and differences

Source: Internet
Author: User
Tags arithmetic

First, Introduction

[equivalent to test, which is the internal command of bash, the coreutils package for the Gnu/linux system usually takes/usr/bin/test and/usr/bin/[commands. If we don't specify the absolute path, we usually use Bash's own commands.

[[is the BASH keyword (which is said to be introduced from 2.02 to [[Support]

Second, similarities and differences analysis of the same point
  1. Arithmetic comparisons are supported ("GT", "GE", "eq", "ne", "LT", "le")
    2 Ten true false true
    2 Ten true false false

    2 Ten true false true
    2 Ten true false false
  2. String comparisons are supported ("<", ">" but somewhat different for specific use)
    About string comparisons. [...]、[[...]] You can compare strings in the order in which they are compared in dictionary order. In terms of ASCII characters, the Code table is arranged in the smaller, such as A<b,a<a, 1<2. Again, as long as the use of "<", ">" is a string comparison, then 9 > 100 is True, because this is actually equivalent to ' 9 ' > ' 100 ', 9 in the Code table after 1, so the string "9" is greater than the string "100".
    2 \< Ten true false false
    2 \> Ten true false true

    Note the use of the escape character \, or bash will consider it a standard output redirect.

    2 < Ten true false true
    2 > Ten true false false
  3. Support for simple pattern matching
    true false true
    true false true
    true false false
    true false true

    true false true
    true false true
    true false false
    true false true

    The pattern matches the extended rule of a wildcard character that resembles a file name. Also note that patterns on the right side of the equal sign cannot be quoted, and special features that use references to turn off certain metacharacters are used.

Different points
  1. Logical AND Logical OR
    1. [Logical AND:-A; logic or:-a]
      [1-lt 2]:true; [B > A]:true
       [email protected]:~$ [1 -lt 2 -A B > a]&&echo true  | | echo false  true  
       [email  protected]:~$ [1 -gt 2 -a b > a]& &echo true  | | echo false  false  
       [email  protected]:~$ [1 -gt 2 -O B > A]& Amp;&echo true  | | echo false  true  
    2. [[Logical AND:&&; logic or: | |]]
      [1-lt 2]:true; [B > A]:true
       [email protected]:~$ [[1 -lt 2  && B > A]]&&echo true  | | echo false  true  
       [email  protected]:~$ [[1 -gt 2  && B > A ]]&&echo true  | | echo false  false  
       [email  protected]:~$ [[1 -gt 2  | | b > A]] &&echo true  | | echo false  true  
  2. Command-line arguments
    "[[" is a keyword that does not do a command-line extension, so in [[Middle < "and" > "does not need to be escaped, but the relative syntax is slightly stricter. For example in [...] You can enclose the operators in quotation marks, because they are removed when you do a command-line extension, and in [[...] It is not allowed to do so
    "-Z" "" true false true
    "-Z" "" true false operator expected-bash:syntax error near ' "'
  3. Arithmetic extension
    [[ ... ]] Perform arithmetic expansion, while [...] No, [...] Need to use $ (())
    +1truefalse+1: integer expression Expectedfalse
    $ ((99+1))  - true false true
    +1truefalsetrue

The basics of Shell programming [and the similarities and differences

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.