Skip to content

Instantly share code, notes, and snippets.

@josepsmartinez
Last active February 14, 2022 21:42
Show Gist options
  • Save josepsmartinez/96787d367fe888b5075f0ba23c83c7a3 to your computer and use it in GitHub Desktop.
Save josepsmartinez/96787d367fe888b5075f0ba23c83c7a3 to your computer and use it in GitHub Desktop.
Bash practices for handling exit codes (Reference: https://stackoverflow.com/a/45817972/4449273)
#!/bin/bash
# In case of simple logging
complex_command && echo "Success" >> log.txt || echo "Failure" >> log.txt
# In case of complex command chaining
complex_command
if [ $? -ne 0]; then
success_complex_command # this command could fail as well
# its exit code can be treated in a nested manner by testing $? again
else
failure_complex_command
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment