The exit code is stored in a special variable: $?
I would suggest storing the exit status in a variable
immediately after executing the command to avoid any confusion. For instance:
Code:
#!/bin/bash
ls /opt > /dev/null
exit_status=$?
if [ ${exit_status} -eq 0 ] ; then
echo "ls command completed successfully"
else
echo "ls failed; exit status of ${exit_status}"
fi