7

I am trying to get the exit code of the last command chained with && and || correctly.

I encountered a strange behavior that I cannot explain. Please help.

This is my exit-code.sh test script:

#!/bin/bash echo "running exit-code with $1" exit $1 

This works as expected:

$ ./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $? running exit-code with 1 last command exit code: 1 

The same with () to run it in a subshell:

$ (./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?) running exit-code with 1 last exit code: 1 

But:

$ /bin/bash -c "./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?" running exit-code with 1 last exit code: 0 

Why do I get exit code 0 here?

2 Answers 2

4

Why do I get exit code 0 here?

The reason is that with the double quotes, parameter expansion (of the special parameter, $?) is carried out before passing arguments to the new Bash process. If you turn on debug and verbose mode, you can see this clearly:

$ set -xv $ bash -c "(./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?)" bash -c "(./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?)" + bash -c '(./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: 0)' running exit-code with 1 last exit code: 0 $ set +xv 

In this case $? is set to 0 because the previous command (set -xv in this example) executed successfully.

3

Quotes

Just use single ' quotes

/bin/bash -c './exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?' running exit-code with 1 last exit code: 1 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.