I was playing around and got stuck while trying to capture the exit code of a script I'm running within a heredoc in a Docker container.
Say I want to run this command to start a Docker container loaded with
python:3.7, and execute a heredoc inside of it.
docker run -v $PWD:/workspace -w /workspace --rm -i python:3.7 <<-EOF ./script.sh echo $? EOF
script.sh looks like this:
echo 'Before' exit 1 echo 'After'
When I run that, I don't get
like I expected, but rather
When I try to assign the exit code to a variable like
./script.sh code=$? echo $code
I get this:
How can I reliably capture the exit code of
script.sh which is run within a heredoc?
I imagine it has something to do with it running in a heredoc in the first place, as running that same sequence of commands within the container directly works fine.