我有这样的bash代码(Mac OS X):
foo.sh | tee foo.log echo $?
问题是$? 包含tee的退出代码而不是foo.sh的退出代码。 我如何获得foo.sh的退出码?
环境variables$PIPESTATUS是pipe道中所有进程的退出状态数组。
也使用一个子shell:
tm@hoegaarden:~$ cat foo.sh #!/bin/bash echo "stuff and junk" exit 123 tm@hoegaarden:~$ (./foo.sh ; echo $? > ./retval ) | tee output stuff and junk tm@hoegaarden:~$ cat retval 123