Bash 中的按位运算

Don*_*nic 2 bash bit-manipulation

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

myprog
BITS=$?
if [ $BITS & 4 ]; then
  echo "Bit 2 set"
fi
Run Code Online (Sandbox Code Playgroud)

建议?

Kam*_*Cuk 5

只需使用 bash 算术扩展:

if (( BITS & 4 )); then
Run Code Online (Sandbox Code Playgroud)