[root@server ~]# bash << eof
> cat adsfasdf
> echo $?
> eof
cat: adsfasdf: No such file or directory
0
谁能解释一下为什么它返回退出状态 0 而不是 1?
答案1
$?
那是因为执行了变量扩展前代码被传递给bash
执行,因此echo
ed 退出代码是之前运行的命令 bash
:
bash-4.1$ ( exit 42 )
bash-4.1$ bash <<EOF
cat asdfasdf
echo $?
EOF
cat: asdfasdf: No such file or directory
42
只需单引号此处文档停止变量扩展的分隔符:
bash-4.1$ bash <<'EOF'
cat asdfasdf
echo $?
EOF
cat: asdfasdf: No such file or directory
1