首页 > 解决方案 > Bash 中的按位运算

问题描述

如何对 Bash 变量进行按位与运算?我想测试程序退出代码中的各个位。就像是:

myprog
BITS=$?
if [ $BITS & 4 ]; then
  echo "Bit 2 set"
fi

建议?

标签: bashbit-manipulation

解决方案


只需使用 bash 算术扩展:

if (( BITS & 4 )); then

推荐阅读