3

Weird one, so the following will not log "bash exit code..."

#!/usr/bin/env bash

OUTPUT_PATH=${PROJECT_ROOT:-$PWD}/npm-install-output.log

npm --loglevel=warn --progress=false install > ${OUTPUT_PATH} 2>&1 &&
export NODE_PATH=${NODE_PATH}:~/.suman/node_modules &&
node $(dirname "$0")/test.js &&

EXIT=$? &&
echo " " && # newline
echo "bash exit code => $?" &&
exit ${EXIT}

if I remove one "&&" after the node command like so:

#!/usr/bin/env bash

OUTPUT_PATH=${PROJECT_ROOT:-$PWD}/npm-install-output.log

npm --loglevel=warn --progress=false install > ${OUTPUT_PATH} 2>&1 &&
export NODE_PATH=${NODE_PATH}:~/.suman/node_modules &&
node $(dirname "$0")/test.js  # <<<<< removed "&&" chars

EXIT=$? &&
echo " " && # newline
echo "bash exit code => $?" &&
exit ${EXIT}

then the node process will exit with a non-zero code, but then bash says:

bash exit code => 0

both of these are not giving correct results, there is something wrong with my code. I want to capture the correct exit code of the node process, and I want to print it out! What could be wrong?

0

1 Answer 1

6

$? is the exit status of your last command. In this case, it always return 0 because echo " " was successfull. Please use $EXIT instead of $?

#!/usr/bin/env bash

OUTPUT_PATH=${PROJECT_ROOT:-$PWD}/npm-install-output.log

npm --loglevel=warn --progress=false install > ${OUTPUT_PATH} 2>&1 &&
export NODE_PATH=${NODE_PATH}:~/.suman/node_modules &&
node $(dirname "$0")/test.js  # <<<<< removed "&&" chars

EXIT=$? &&
echo " " && # newline
echo "bash exit code => $EXIT" &&
exit ${EXIT}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .