[ a = a -a b = b ]:几乎等效,但被POSIX弃用,因为它是疯狂的,并且对于a或b的某些值失败,例如!或(,这将被解释为逻辑操作
(
[[ (a = a || a = b) && a = b ]]:false。如果没有( ),它将是true,因为[[ && ]]比[[ || ]]具有更大的优先级
[ ( a = a ) ]:语法错误,()被解释为子shell
[ \( a = a -o a = b \) -a a = b ]:等效,但()、-a和-o被POSIX弃用。如果没有\( \),这将是真的,因为-a比-o具有更大的优先级
{ [ a = a ] || [ a = b ]; } && [ a = b ]非弃用的POSIX等价物。然而,在这个特定的情况下,我们可以只写:[ a = a ] || [ a = b ] && [ a = b ],因为||和&& shell运算符具有相同的优先级,与[[ || ]]和[[ && ]]以及-o、-a和[不同
扩展时的单词拆分和文件名生成(拆分+全局)
x='a b'; [[ $x = 'a b' ]]: true.不需要引号
x='a b'; [ $x = 'a b' ]:语法错误。它扩展到[ a b = 'a b' ]
x='*'; [ $x = 'a b' ]:如果当前目录中有多个文件,则出现语法错误。
x='a b'; [ "$x" = 'a b' ]:POSIX等效
=
[[ ab = a? ]]:true,因为它确实模式匹配(* ? [是魔术)。不会全局扩展到当前目录中的文件。
[ ab = a? ]:a?全局扩展。因此它可能为真或假,具体取决于当前目录中的文件。
[ ab = a\? ]:false,不是全局扩展
=和==在[和[[中是相同的,但==是Bash扩展。
case ab in (a?) echo match; esac:POSIX等效
[[ ab =~ 'ab?' ]]:false,在Bash 3.2及更高版本中''失去魔力,并且未启用与Bash 3.1的兼容性(如BASH_COMPAT=3.1)
# This ensures the string on the left is made up of characters in# the alnum character class followed by the string name.# Note that the RHS should not be quoted here.if [[ "filename" =~ ^[[:alnum:]]+name ]]; thenecho "Match"fi
# This matches the exact pattern "f*" (Does not match in this case)if [[ "filename" == "f*" ]]; thenecho "Match"fi
# This gives a "too many arguments" error as f* is expanded to the# contents of the current directoryif [ "filename" == f* ]; thenecho "Match"fi