0

Say I have a command that looks similar to the following:

VAR=$(python SomeScript | tee /dev/null)

I would like to get the exit code of python script but not really sure how with the assignment being in the same command.

1
  • 2
    tee /dev/null is pretty useless, what are you trying to do with this ? Commented Jun 8, 2020 at 21:46

2 Answers 2

2

If you only have a single exit code to return, you can extract and exit with it to make that the exit code of the whole command substitution:

var=$(
  python -c 'import sys; print("hi"); sys.exit(42)' | cat
  exit "${PIPESTATUS[0]}"
)
ret=$?
echo "The output is $var and the exit code is $ret"

This results in:

The output is hi and the exit code is 42

If you need to extract multiple exit statuses, you'll have to write them to a file or the end of the stream, and then read them back or extract them afterwards.

Sign up to request clarification or add additional context in comments.

Comments

0

Like this:

var="$(python SomeScript)" >/dev/null
echo "SomeScript exit code: $?"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.