Bash exit code



examples/bash/cond_exit_code.sh
#!/usr/bin/env bash

ls  / > /dev/null 2> /dev/null
exit_code=$?
echo $exit_code
if [ $exit_code = 0 ]; then
    echo /
fi

ls  /qqrq > /dev/null 2> /dev/null
exit_code=$?
echo $exit_code
if [ $exit_code = 0 ]; then
    echo /qqrq
fi

Make sure you save the exit code immediaely after the execution of some code if you'd like to use it later, as it always contains the exit code of the most recent statement.