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
Suppose script.sh
looks like this:
echo 'Before'
exit 1
echo 'After'
When I run that, I don't get
Before
1
like I expected, but rather
Before
0
When I try to assign the exit code to a variable like
./script.sh
code=$?
echo $code
I get this:
Before
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.