推荐使用 [[...]] ,而不是 [ , test , 和 /usr/bin/[ 。 因为在 [[和]] 之间不会有路径名称扩展或单词分割发生,所以使用 [[...]] 能够减少错误。而且 [[...]] 允许正则表达式匹配,而 [ ... ] 不允许。
# 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.
# For the gory details, see
# E14 at http://tiswww.case.edu/php/chet/bash/FAQ
if [[ "filename" =~ ^[[:alnum:]]+name ]]; then
echo "Match"
fi
# This matches the exact pattern "f*" (Does not match in this case)
if [[ "filename" == "f*" ]]; then
echo "Match"
fi
# This gives a "too many arguments" error as f* is expanded to the
# contents of the current directory
if [ "filename" == f* ]; then
echo "Match"
fi