Q:

Why Do I Have No Exit Code From debootstrap?

I am testing out debootstrap in a chroot via a bash script.
The snippet that runs it is:
#!/usr/bin/env bash
set -e
...
...
sudo debootstrap --verbose --arch=${ARCH} ${RELEASE} ${chroot_dir}
if [ "$?" -ne "0" ]; then
  echo "debootstrap failed."
  echo "See ${chroot_dir}/debootstrap/debootstrap.log for more information."
  exit 1
fi

echo "debootstrap succeeded"

However, after running the script the last lines printed to the terminal are:
I: Chosen extractor for .deb packages: dpkg-deb
I: Extracting adduser...

I don't see the failed or the succeeded message.
The debootstrap log in the chroot says:
tar: ./usr/sbin/addgroup: Cannot create symlink to 'adduser': File exists
tar: ./usr/sbin/delgroup: Cannot create symlink to 'deluser': File exists
tar: Exiting with failure status due to previous errors

So clearly something has failed.
Why am I not able to catch this in my error check block?

A:

If you just want to test for an exit code, bash has the following simple syntax:
if debootstrap --verbose --arch=${ARCH} ${RELEASE} ${chroot_dir}
then
  echo Success
else
  echo Failure
fi

The reason why the other way doesn't work is because set -e causes the script to exit before the exit code is tested.
For further reading about exit values and error trapping see the following links:

http://mywiki.wooledge.org/BashFAQ/105
https://sanctum.geek.nz/arabesque/testing-exit-values-bash/

