考虑:
$ read -r a <<<$(echo "foo"; exit 1)
$ echo $?
0
当我真正期望 1 时,这会返回 0。如何从子 shell 中提取真正的退出代码?
答案1
您将需要多个步骤:
output=$(echo "foo"; exit 1)
status=$?
read -r a <<<"$output" # assuming the "real" code here is more complex
考虑:
$ read -r a <<<$(echo "foo"; exit 1)
$ echo $?
0
当我真正期望 1 时,这会返回 0。如何从子 shell 中提取真正的退出代码?
您将需要多个步骤:
output=$(echo "foo"; exit 1)
status=$?
read -r a <<<"$output" # assuming the "real" code here is more complex