I would recommend writing the integer value to a file. I completely agree that exit codes should not be used for passing any information, other than success or failure of the program.
For example b.sh:
#! /bin/bash
set -ex
# Create temp file, ensure it is erased on bash script exit
tempfile="$(mktemp)"
trap 'rm -f "$tempfile"' EXIT
trap 'exit 1' HUP INT QUIT PIPE TERM
# Run program and write value to temp file
python3 ./a.py -o "$tempfile"
# Read value from temp file
myvalue="$(cat "$tempfile")"
echo "Value from a.py was: $myvalue"
And a.py:
#! /usr/bin/env python3
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument(
"-o",
"--output-file",
help="Write integer value to text file",
type=str,
required=True,
)
args = parser.parse_args()
my_value = 42
Path(args.output_file).write_text(str(my_value))
set -e.a.py||false, but what's the point in returning a certain exit code, if you don't want to evaluate it?