Hasty Briefsbeta

Bilingual

The one-line mistake that makes bash scripts fail silently

6 hours ago
  • #production reliability
  • #bash scripting
  • #error handling
  • Bash scripts can fail silently, leading to undetected backup failures and other issues, especially in automated environments.
  • Use 'set -euo pipefail' as a foundation to stop on errors, undefined variables, and pipe failures, but be aware of its limitations.
  • Implement 'trap' for cleanup to ensure temporary files and resources are removed regardless of how the script exits.
  • Always quote variables to prevent whitespace splitting and globbing, avoiding common bugs in production.
  • Validate dependencies, arguments, and environment before acting to prevent failures in different setups.
  • Write detailed logs with timestamps and context to aid troubleshooting, especially during off-hours incidents.
  • Use file locking with 'flock' to prevent concurrent execution and data corruption in scheduled scripts.
  • Utilize ShellCheck for static analysis and consider Bats for testing to catch errors before deployment.
  • Conduct a final pre-production check including flags, cleanup, quoting, validation, logging, concurrency protection, and ShellCheck results.
  • Share experiences and tips in communities to learn from common bash script mistakes and improve reliability.