Bash - case ;;&

语法

case word in
[ [(] pattern [| pattern]…) command-list ;;]…
esac

command-list 后面的 ;; 其实还可以换成 ;&;;&, 它们有什么区别呢 ?

实例 ;&

;& 是在匹配到 pattern 后, 继续执行 next clause 的 command-list, 比如脚本:

ANIMAL=$1
echo "The $ANIMAL has "
case $ANIMAL in
horse | dog | cat) echo "four ";&
horse_arab ) echo "(4)";;
human ) echo "two ";;
*) echo "an unknown number of ";;
esac
echo "legs."

结果:

➜  ./foo horse     
The horse has
four
(4)
legs.

➜ ./foo horse_arab
The horse_arab has
(4)
legs.

如上 (4) 会在 four 之后出现.

实例 ;;&

;;& 表示 as if the pattern list had not matched before.

ANIMAL=$1
echo "The $ANIMAL has "
case $ANIMAL in
horse | dog | cat) echo "four ";;&
human ) echo "two ";;
horse ) echo "big ";;
*) echo "an unknown number or size of ";;
esac
echo "legs."

结果:

➜  ./foo horse
The horse has
four
big
legs.

➜ ./foo dog
The dog has
four
an unknown number or size of
legs.

可以看到 :
dog 会输出 fouran unknown number or size of, ;;& 会把 dog 当作没有匹配到而继续往下进行匹配.
horse 会输出 fourbig.

实例 ;;

把上面的 ;;& 改为 ;; 后, 则匹配到之后退出 case 块:

➜  ./foo horse
The horse has
four
legs.

参考

https://www.gnu.org/software/bash/manual/bash.html