Concept Flow - Why error handling prevents silent failures
Start Script
Run Command
Check Exit Status
Continue
End
End
The script runs a command, checks if it worked, and if not, shows an error and handles it to avoid silent failures.
cp source.txt dest.txt if [ $? -ne 0 ]; then echo "Copy failed!" fi
| Step | Action | Command Exit Status ($?) | Condition | Branch Taken | Output |
|---|---|---|---|---|---|
| 1 | Run cp source.txt dest.txt | 0 | 0 != 0? | No | |
| 2 | Check if last command failed | 0 | No | Continue | |
| 3 | Script ends |
| Variable | Start | After cp | After if check | Final |
|---|---|---|---|---|
| $? | N/A | 0 | 0 | 0 |
Run a command in bash. Check its exit status with $?. If non-zero, handle error (e.g., print message). Prevents silent failures by alerting user. Always check $? after important commands.