Linux shell programming string compare syntax

后端 未结 4 975
小蘑菇
小蘑菇 2021-02-12 18:47

What is the difference between = and == to compare strings in Linux shell programming?

Maybe the following code works:

if [ \"$         


        
4条回答
  •  一向
    一向 (楼主)
    2021-02-12 19:28

    In general, the = operator works the same as == when comparing strings.

    Note: The == comparison operator behaves differently within a double-brackets test than within single brackets.

    [[ $a == z* ]]   # True if $a starts with an "z" (pattern matching).
    [[ $a == "z*" ]] # True if $a is equal to z* (literal matching).
    
    [ $a == z* ]     # File globbing and word splitting take place.
    [ "$a" == "z*" ] # True if $a is equal to z* (literal matching).
    

    source: http://tldp.org/LDP/abs/html/comparison-ops.html

提交回复
热议问题