Bash 如何执行所有命令,但 return 上一个命令的退出值

Bash how to execute all commands but return exit value of previous command

我想测试是否成功安装了 c++ 库,所以我有一个命令链:

$ echo '#include "library_to_check_for"' > test.cpp && g++ test.cpp ??? rm test.cpp ???

每次都需要删除命令但我需要 $?到 return 编译命令的状态(即编译成功&是否找到库)

从逻辑上讲,我认为有一种方法可以 AND/OR 让它发挥作用。我只是无法理解它

可能是这个..?

$ echo '#include "library_to_check_for"' > test.cpp; g++ test.cpp; rm test.cpp; exit (something?)

也许您可以像这样将 return 值存储在变量中?

echo '#include "library_to_check_for"' > test.cpp; g++ test.cpp; LASTRETURN=$?; rm test.cpp;
echo $LASTRETURN