
이 함수는 호출 스크립트를 종료해야 합니다.
crash() {
echo error
exit 1
}
이는 예상대로 작동합니다.
echo before
crash
echo after # execution never reaches here
하지만 다음은 그렇지 않습니다.
echo before
x=$(crash) # nothing is printed, and execution continues
echo after # this is printed
함수의 결과를 캡처하고 종료하려면 어떻게 해야 합니까?
답변1
이는 서브셸에서 $(crash)
실행되므로 스크립트가 아닌 서브셸에 적용되기 crash
때문 입니다.exit
어쨌든 스크립트가 종료되므로 변수가 사용되지 않을 경우 변수에 출력을 캡처하는 이유는 무엇입니까?
답변2
이렇게 하면 문제가 해결됩니다.
echo before
x=$(crash) || exit # if crash give -gt 0 value then exit with the same value
echo after